1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

need help with itunes applescript

Discussion in 'Mac Programming' started by twoodcc, Feb 19, 2007.

  1. macrumors P6

    twoodcc

    #1
    ok, i have taken some programming classes and stuff, but never anything with applescript.

    i downloaded a script from here and the one i need help with is called 'Needle Drop'.

    but i want to change it a little. instead of asking the user how many seconds and where to start playing, i want it to ask the user how many seconds, and then it start playing for that many seconds from the end of the track. does that make sense?

    so, let's say you put in 10 seconds.....then it will play the last 10 seconds of the track.

    thanks in advance
     
  2. macrumors P6

    twoodcc

    #2
    basically, how do you find the length of the track? this seems like it would be easy if you know the length of it, then whatever the user inputs, it subtracts it from the length...
     
  3. macrumors P6

    twoodcc

    #3
    well so far i have this:

    Code:
    global start_time
    global needle
    
    on run
    	tell application "iTunes"
    		activate
    		set needle to 5
    		set v to sound volume
    		copy 0 to sound volume
    		if selection is not {} then
    			play selection
    		else
    			play view of front window
    		end if
    		pause
    		copy v to sound volume
    		
    		set start_time to time of current track
    		try
    			return (start_time as integer)
    		end try
    		
    		set needle to start_time - needle
    		set player position to needle
    		play
    	end tell
    end run
    on quit
    	tell application "iTunes" to stop
    	continue quit
    end quit
    but i get the error " can't change 4:56 to number"
    or something like that.

    what can i do to fix it?

    thanks
     
  4. macrumors P6

    twoodcc

    #4
    ok well i got it work! but how do i tell it to repeat until i quit the apple script?
     
  5. macrumors P6

    twoodcc

    #5
    well i can get it to repeat, but how do i tell the script to pause for a number of seconds? (not pause itunes)
     
  6. macrumors P6

    twoodcc

    #6
    well i got my program to work! yea!

    but only one problemo.....i can't get it to quit! i put 2 buttons on it, start and stop, but i can't click the stop button!

    please help me here

    Code:
    global start_time
    global needle
    
    
    
    on clicked theObject
    	if title of theObject = "Start" then
    		repeat
    			tell application "iTunes"
    				activate
    				set needle to 5
    				set start_time to (duration of current track) - needle
    				
    				set player position to start_time
    				play
    				
    			end tell
    			delay 6
    			if title of theObject = "Stop" then
    				quit
    			end if
    		end repeat
    	else if title of theObject = "Stop" then
    		quit
    	end if
    	
    end clicked
    
    on quit
    	tell application "iTunes" to stop
    	continue quit
    end quit
     
  7. macrumors P6

    twoodcc

    #7
    well i finally got it to work! i just had to change quit to 'exit repeat' in one of the if statements.

    when i start the program , i get an error message though, but it still works.

    i'm kinda disappointed that no one helped me though
     
  8. macrumors P6

    twoodcc

    #8
    is there any way to make my program universal binary?
     
  9. macrumors P6

    twoodcc

    #9
    n/m, i figured it out on my own.....again ;) :apple:
     
  10. macrumors 6502a

    lancestraz

    #10
    To the OP: Good thing twoodcc was here to help you! You should thank him. :p
     
  11. macrumors P6

    twoodcc

    #11
    LOL, you're right, i should :p

    but he still couldn't help me fix the error that i get though. -1708 or something.

    would love some help with that :):apple:
     
  12. macrumors P6

    twoodcc

    #12
    also, when i have my script going, i basically can't do anything else, b/c every time it repeats, it makes itunes come to the front. is there any way to make it run in the background, so i can use other applications at the same time?

    thanks
     
  13. macrumors 68000

    bearbo

    #13
    don't put activate in the repeat
     
  14. macrumors 6502a

    lancestraz

    #14
    OK, OK. I'll try to help too.

    In interface Builder, you need to add a "Wake from nib" event handler to the main window.
    Then add this:
    Code:
    on awake from nib theObject
    	tell application "iTunes"
    		launch
    	end tell
    end awake from nib
    That should stop iTunes from jumping to the front.

    As for the error. It's going sour because no song is playing in iTunes.
    To solve that add this right inside the repeat > tell iTunes statement.
    Code:
    if selection is not {} then
         play selection
    else
         play view of front window
    end if
    So in the end it should look like this:
    Code:
    global start_time
    global needle
    
    on clicked theObject
    	if title of theObject = "Start" then
    		repeat
    			tell application "iTunes"
    				if selection is not {} then
    					play selection
    				else
    					play view of front window
    				end if
    				set needle to 5
    				set start_time to (duration of current track) - needle
    				set player position to start_time
    				play
    			end tell
    			delay 6
    		end repeat
    	else if title of theObject = "Stop" then
    		quit
    	end if
    end clicked
    
    on awake from nib theObject
    	tell application "iTunes"
    		launch
    	end tell
    end awake from nib
    
    on quit
    	tell application "iTunes" to stop
    	continue quit
    end quit
    I also got rid of the:
    "if title of theObject = "Stop" then
    quit
    end if"
    that was inside the repeat loop. You don't need it.
     
  15. macrumors P6

    twoodcc

    #15
    thanks :)

    thanks! i'll try it and let you know how it turns out. :) :apple:
     
  16. macrumors P6

    twoodcc

    #16
    well i still get the error, but it doesn't pop up itunes everytime now :) so i'm thankful for that.

    i didn't take out the if statement like you did, so maybe that's the error. i'll try it when i get out of class
     
  17. macrumors P6

    twoodcc

    #17
    well using your method, i can't stop the program. i can't click the stop button, and i can't click the x at the top left. any ideas?
     
  18. macrumors 6502a

    lancestraz

    #18
    That's weird.
    Try downloading this. (my xcode project), and see if it works.
     
  19. macrumors P6

    twoodcc

    #19
    thanks, i'll try it and let you know :)
     
  20. macrumors P6

    twoodcc

    #20
    well yours works! i compared the two, and they are exactly the same, or seem to be. very weird. thanks though. i'm just glad it works :) :apple:
     
  21. macrumors 6502a

    lancestraz

    #21
    Maybe you forgot to tick some checkbox, or whatnot, in Interface Builder.
    At least it works now. :)
     
  22. macrumors P6

    twoodcc

    #22
    that's what i thought also, and i compared yours and mine, and they look the same for the buttons. maybe there's something somewhere else i forgot to look.

    yeah, thanks :)
     

Share This Page