I'm trying to write a loop script and I've lost my self in to it. Help

Hello , I’m new in Apple script , I think it’s very powerfull but I can’t understand it very much . I’m not a programmer eather , but I need a simple code .

My intention it’s to make a script that open a location every X second but the code that I wrote it take cpu to 100% and it can’t quit the script .

I need iTunes open a location to see the pod cast’s Top 20 chart every X seconds , but with my BAD and Simple code I’ve this insue :

Can’t quit applescript
Some time iTunesMusic store tell me error so I’ve to manualy click the pront to let it back to work
iTunes.app come in front every call open location
If I’m running the AS in iTunes Script Folder iTunes don’t respond any more

This is my bad code , can you help me please ?



ignoring application responses
	
	
	tell application "iTunes"
		
		run
		
		repeat
			
			open location "itms://phobos.apple.com/WebObjects/MZStore.woa/wa/viewGenre?id=26"


			delay 60
		end repeat
		
		
		
	end tell
	
end ignoring


Nearly trashed my itunes with this script.
you either have to watch the frequency with which it scans ITMS or it may have caused my router to have indigestion!
Either way be careful!!!
my version is:
ignoring application responses
repeat

	tell application "iTunes"
		run
		activate
		
		open location "itms://phobos.apple.com/WebObjects/MZStore.woa/wa/viewGenre?id=26"
	      --give itunes time to download the page	
                 delay 40
		quit
	end tell
          -- delay before script runs again
	delay 20
end repeat

end ignoring

Don’t understand your other problems with itunes coming to the front etc? How else can you see what you want to see?

it seams to like mine … I don’t like iT quit and run all time .

I still have same problems

Hi usage of CPU
Can’t quit the script

I’m not a script writer so may be my version it’s complitly wron , but you now understand what I would like to do

Suggestions ?

How frequently do you want to run the script. There are shareware apps such as cronnix which can schedule a script to run at certain times on selected days. It is really just a user friendly frontend to the cron function in unix.
This is the only way to stop and start the script itself otherwise it must run all the time. If you remove the “Quit” line from the script then ITunes will be open all the time. If you remove the “activate” then it will not come to the front, but then how can you see the itunes info that you talked about?
I can show you a cronnix sample command to run an applescript every weekday at a fixed time if you think that would help.
riverman

I would love to run the script all the time , but I need to quit it if I want .

The problems are :

Hi usage of CPU
Can’t quit the script

I need to know how to quit the script

and why it use so hi percente of CPU

It might be better to use an on idle handler

on idle
-- your actions
end idle

because using

repeat
-- do actions
end repeat

is an endless loop and will continue running until you stop it. If you use the idle handler, you can set a time to check periodically and test that with an if statement. If the time meets your criteria then you perform your actions.



on idle
	tell application "Finder"
		set theTime to the time of (current date)
		set testDate to (current date) - theTime -- the current day at 12:00 AM
	end tell
		if time of (current date) is greater than or equal to 21600 then -- 6:00 AM current day
			tell application "iTunes"
				open location "itms://phobos.apple.com/WebObjects/MZStore.woa/wa/viewGenre?id=26"
			end tell	
		end if
end idle

I haven’t tried the above code so there may be errors. Save this as a stay open application (button at the bottom of the save as dialog box).

PreTech

Model: dual 1.8 G5
AppleScript: 2.1 (80)
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

I forgot to tell you that the repeat statement is usually of the form:

repeat with i from 1 to something
end repeat

or

repeat with anItem in something
end repeat

This way the loop will eventually end on its own.

PreTech

I’m sorry but I’m don’t know about AS .

Should a Set the data manually ?

How this works ?

I need a script that loop every 20 / 30 senconds … that’s it

Thanks

Add a delay 20 to this.

on idle
   tell application "Finder"
       set theTime to the time of (current date)
       set testDate to (current date) - theTime -- the current day at 12:00 AM
   end tell
       if time of (current date) is greater than or equal to 21600 then -- 6:00 AM current day
           tell application "iTunes"
               open location "itms://phobos.apple.com/WebObjects/MZStore.woa/wa/viewGenre?id=26"
           end tell    
       end if
delay 20
end idle

You then save this as a stay open application. Then you double click it to start it. An icon will show up in your dock and you can quit it like any other program.

PreTech

:d:d:d:d:d I Love Simple Words !!! Ahahahah Yes Thanks ! Thanks A Lot !

One more thing . I need to quit iTunes every X minute .

Some time itms give me back some error message , the simplest way to let the script work in automatic it’s to quit iTunes every 5 / 6 minute . There is any way to do this ?

Try this:

property elapsedTime : 600 -- sets the variable elapsedTime to 600 seconds. 
on idle
	tell application "Finder"
		set theTime to the time of (current date)
		set testDate to (current date) - theTime -- the current day at 12:00 AM
	end tell
	if time of (current date) is greater than or equal to 21600 then -- 6:00 AM current day
		tell application "iTunes"
			open location "itms://phobos.apple.com/WebObjects/MZStore.woa/wa/viewGenre?id=26"
		end tell
	end if
	delay 20
	if the (time of (current date)) is greater than or equal to (theTime + elapsedTime) then
		tell application "iTunes"
			quit
		end tell
	end if
end idle

PreTech

How about this?:

property timeInterval : 60 -- in seconds
property theURL : "itms://phobos.apple.com/WebObjects/MZStore.woa/wa/viewGenre?id=26"
property downloadTime : 40 -- in seconds

on run
	set justDone to false
end run

on idle
	if justDone then
		quit application "iTunes"
		set justDone to false
	else if time of (current date) is greater than or equal to 21600 then -- 6:00 AM
		tell application "iTunes" to open location theURL
		set justDone to true
		return downloadTime
	end if
	return timeInterval
end idle

Qwerty Denzel,

This is cool, but please explain the two return statements. I’m not understanding what they’re doing.

PreTech

They return the amount of time to wait before idling again.

Qwerty Denzel,

So are timeInterval and downloadTime special operators in AS? I ask this because I don’t understand how they function without more code involving their use. And if they are special operators where can I find info about other special operators?

Thanks.

PreTech

timeInterval and downloadTime are properties defined at the beginning:

property timeInterval : 60 -- in seconds
property downloadTime : 40 -- in seconds

-- rest of script.

I hate to keep bothering you, but I’ve got to ask again. I understand for the most part the

property timeInterval : 60

but what I’m not understanding is how the script knows this number is in seconds as opposed to milimeters or any other units and that it essentially needs to count 60 seconds (or however long). How does it know to wait for “x” many seconds?

PreTech

The number returned in the on idle handler is treated the same way as delay. Units are in seconds.
So to wait 5 minutes you use:

return 5 * minutes

.much in the same way as:

delay 5 * minutes

That explains it. Thanks! :smiley: