Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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...
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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
 

bearbo

macrumors 68000
Jul 20, 2006
1,858
0
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

don't put activate in the repeat
 

lancestraz

macrumors 6502a
Nov 27, 2005
898
0
RI
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:

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

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.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
don't put activate in the repeat

thanks :)

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.

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

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
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
 

lancestraz

macrumors 6502a
Nov 27, 2005
898
0
RI
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:
Maybe you forgot to tick some checkbox, or whatnot, in Interface Builder.
At least it works now. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.