playing with applescript and hitting a wall

Discussion in 'Mac Programming' started by homeshire, Mar 17, 2007.

  1. macrumors regular

    Joined:
    Jul 24, 2002
    Location:
    Ohio, looking toward Germany
    #1
    I admit it. I'm doing it just for the fun of it. Now I can't figure out how to get around this wall.
    For years I thought it would be cool if I could pick a song from the desktop in some manner on a whim and hear it launched in iTunes.
    Recently, I started looking at applescript, mostly just playing and trying to have fun while learning a thing or two. That's when I decided to really try something courageous. After 3-4 days of reading the applescript missing manual, I've gotten this far in my script:

    set mymsg to "what'cha wanna hear?"
    set myresponse to the text returned of (display dialog mymsg default answer "" buttons ["Got it!", "Not right now, thanks!"] default button 1)
    set mychoice to the button returned of (display dialog myresponse buttons ["Here we go!", "Uh, there's a problem!"] default button 1)
    repeat (1) times
    tell application "iTunes"
    play track mychoice of playlist "KoWeiNaLie"
    end tell
    end repeat

    I know it may not be particularly elegant. But I've got it to work right up to playing the track. At that point, the script craps out and gives me an error -- saying it can't make some data into the expected type. I'm pretty proud of myself having gotten that far. But guess I'm too dull to see my errorl

    Can someone point out what I'm probably just not seeing? Thanks
     
  2. macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #2
    Code:
    set mymsg to "what'cha wanna hear?"
    set myresponse to the text returned of (display dialog mymsg default answer "" buttons ["Got it!", "Not right now, thanks!"] default button 1)
    set mychoice to the button returned of (display dialog myresponse buttons ["Here we go!", "Uh, there's a problem!"] default button 1)
    repeat (1) times
    	tell application "iTunes"
    		play track [B]mychoice[/B] of playlist "KoWeiNaLie"
    	end tell
    end repeat
    The bolded part is the culprit.
    "mychoice" is set to the name of a button.
    Change it to "myresponse" to get it to work.
    Also, the repeat is unnecessary.


    If you want to learn more AppleScript you can take a look at this example which basically does the same thing except it can tell if you didn't enter anything in the text box or the song doesn't exist:
    Code:
    --Set the default dialog.
    set the_dialog to "What'cha wanna hear?"
    set the_icon to 1
    
    tell application "iTunes"
    	activate
    	
    	--Keep repeating until the user gets it right or quits.
    	repeat
    		
    		--Display the dialog requesting the name of a song.
    		display dialog the_dialog default answer "" buttons {"Not Right Now", "Got It"} default button 2 with icon the_icon
    		
    		--Map the text entered and the button clicked to variables.
    		set the_result to the result
    		set the_song to the text returned of the_result
    		
    		--Run this if the users clicks "Got it". Otherwise, quit.
    		if the button returned of the_result is "Got It" then
    			
    			--If the user didn't enter anything in the text box, change the text the dialog displays and ask again
    			if the_song is "" then
    				set the_dialog to "You have to enter the name of a song!"
    				set the_icon to 0
    			else
    				
    				--Check to see if the song exists.
    				if track the_song of playlist "Library" exists then
    					
    					--Display an affirmative dialog.
    					display dialog "Play: \"" & the_song & "\"?" buttons {"No", "Yes"} default button 2 with icon 1
    					if the button returned of the result is "Yes" then
    						
    						--Play the blasted song.
    						play track the_song of playlist "Library" with once
    						exit repeat
    					else
    						
    						--If the user click no the reset the dialog back to default
    						set the_dialog to "What'cha wanna hear?"
    						set the_icon to 1
    					end if
    				else
    					
    					--If the song doesn't exist then change the dialog box to tell the user.
    					set the_dialog to "That song doesn't seem to exist!" & return & "Make sure you spelt it right and try again."
    					set the_icon to 0
    				end if
    			end if
    		else
    			exit repeat
    		end if
    	end repeat
    end tell
     
  3. thread starter macrumors regular

    Joined:
    Jul 24, 2002
    Location:
    Ohio, looking toward Germany
    #3
    Thanks Lancestraz -

    you've found my flaw, and I've tested yours, which works juso so, and much more elegantly. I'll study both to try to learn more. I knew I was being at least redundant with the repeat, but I also knew it wasn't hurting. As I said, I've only been at this this past week, so I hope to get better. Again, thank you for posting and helping me.
     

Share This Page