newbie - URL Request

Hello, I admit to be a total newbie to AppleScript. After playing with it for a bit, I can tell it is quite powerful. I’m more of a webguy though, and I was attempting to do some integration.
I stumbled upon an applescript that takes the song name you are listening to and sends it to AIM. I want to take the song name and send it to a url.
For example, go to http://www.blah.net/song.php?song=song%20name&artist=artist%20name
From there I’d do the rest of the work, inserting it into a little mySQL table where it would be displayed on the front page.
I’d love for this to run without using a browser, but I understand that may be impossible…
Anyways, I thought it would be worth a shot, and was hoping someone could give me some insight.
Thanks!
Kenton Gray

hmm that sounds like exactly what i had in mind, but confound my luck I need it to work in OS X.

Thanks for all your help!

If anyone knows any type of replacement for this function in OS X, don’t be a stranger :slight_smile:

Kenton

ok now i’m responding to myself, I decided what the heck, even though it was mentioned url access scripting doesn’t work in X, i’d try anyways. To my pleasant surprise it worked great!
So here is what I have:

repeat
set the theTime to (the (current date) + 60)
repeat until the (current date) is greater than theTime
end repeat
try
tell application "iTunes"
set artist to (artist of current track)
set song to (name of current track)
end tell
on error
set theSong to "(no song playing)"
end try
try
tell application "URL Access Scripting"
set the temporaryFile to "/Users/kenton/test.php"
activate
download "http://www.hiddenfromeveryone.com/song.php?song=" & song & "&artist=" & artist & "" to temporaryFile without progress
quit
end tell
end try 
end repeat

Now bare in mind I basically cut and paste together random peices of code and just kinda made it up as I go, but everything seems to work. The php script parses it and puts it right on my site!
So now, just 2 issues! great!

  1. Every time it runs for a second it takes over my computer, so I’m typing in this and then take control then give back. Just enough to be a slight problem, anything I can do about that?
  2. It creates an excess of these temp files, anyway I can delete the file right after it creates them?
    I really have to say thanks to everyone here! you guys have been great!

This could be a result of

set the theTime to (the (current date) + 60)
repeat until the (current date) is greater than theTime
end repeat

Repeats usually take over a script and other processes, so for a more effective pause of 60 seconds try “delay 60” which simply pauses the script execution for 60 seconds. Or investigate a slick osax called Sleep Commands. Depending on why you need to pause execution it may provide a better solution. sigh But this too is another step into Classic dependance (until osaxen in X are embraced).
Also, URLAS is a little slow with process switching, so if it works without the ‘activate’ this may coax it into downloading in the background without taking over the computer.

I sometimes use this method of updating a db with a siphonscript page, and when I don’t need the result I save the temporary files into OS 9’s invisible Temporary Items Folder. They are then automatically deleted (usually). If this isn’t available, look to the Finder’s scripting dictionary for file manipulation - such as ‘tell application “Finder” to delete temporaryFile’ which should move the files to the Trash, which can then be emptied by another command if so desired.
My apologies for the tennative tone. OS X is all new territory for me, I’ve been saving my pennies for a new machine, and a lot of well known scripting techniques have changed dramatically. I suspect that a URL Request in X might be achievable with Terminal commands, and without the Terminal launching using the ‘do shell script’ AppleScript command…but I’ve got zero experience in that area (‘wget’ is striking a bell in my head, dunno why).

OH MY GOSH!!!
I was totally mistaken to think that URLAS is no longer with us in Mac OS X.
http://www.applescriptsourcebook.com/applescript/applescript170.html
It’s there, and hasn’t changed. Cool. It’s good news for a lot of my scripts (and yet still more incentive to splurge ahead of budget).
Depending on your server setup, some of the new Web Services may come in handy.
http://www.apple.com/applescript/macosx/web_services/

Wow I have to say, you can’t buy support this good :slight_smile:
Thanks to you guys, everything works great!
If anyone wants to see my script in action you are more than welcome to check out my little site, www.pocketlint.net; everything is still really early, but all the script no longer takes control of everything and its really concise.
for the curious here is the completed [for now] code!

repeat
delay 10
try
tell application "iTunes"
set theArtist to (artist of current track)
set theSong to (name of current track)
end tell
on error
set theSong to "(no song playing)"
end try
try
tell application "URL Access Scripting"
set the temporaryFile to "/Users/kenton/tmp/test.php"
download "http://www.pocketlint.net/song.php?song=" & (theSong) & "&artist=" & (theArtist) & "" to temporaryFile without progress
quit
end tell
end try 
end repeat

and if anyone would like to see the php and mysql backend feel free to email me!
Again, thanks guys, if any of you need any help on any php or anything else, I’ll be glad to assist!

Kenton,

I thought the following might be helpful. I took your code from the “finished - for now” post, and added a few things. First, I separated a few things into handlers, so the code is somewhat easier to read. Then, I put in an idle handler (rather than using a delay-repeat loop). If you take this script, save it as a Stay-Open application from Script Editor, it will run every ten seconds. The ‘on idle’ handler does the following:

When your applet is idle, it runs the idle handler. The idle handler should return the number of seconds the applet should wait in idle mode before running the idle handler again. I basically says “OK, I’ve done some stuff, and now I’m going to give myself an X second break before I do anything again.” (where X is the number returned by the idle handler). Take a look at the code below, and see if it makes sense.

on run
	sendSong()
end run

on idle
	sendSong()
	return 10 -- number of seconds before next time it does something
end idle

on sendSong()
	try
		set myURL to getURL() -- get the URL from iTune's current song
		tell application "URL Access Scripting"
			set the temporaryFile to "/Users/krioni/tmp/test.php"
			download myURL to temporaryFile replacing yes without progress
			quit
		end tell
	end try
end sendSong

on getURL()
	try
		tell application "iTunes"
			set theArtist to (artist of current track)
			set theSong to (name of current track)
		end tell
	on error
		set theSong to "(no song playing)"
		set theArtist to "(no song playing)"
	end try
	return "http://www.pocketlint.net/song.php?song=" & (theSong) & "&artist=" & (theArtist) & ""
end getURL

Do idle handlers work in OSX? For example, save the following as a stay open application:

 on idle 
 -- your stuff goes here 
 tell application "Finder" 
  say (time string of (current date)) 
 end tell 
 -- end your stuff 
 return 60 
end idle 

gl, Kel.

If you are working out of ‘X’ there is the lovely Akua “pause for” - lovely because it doesn’t lock up the CPU.

Also, the idle handler is still probably the best way to go, since it uses built-in AppleScript, and runs on most version of AppleScript.

I agree that an idle handler is the best idea.