Hi, i have an AppleScript that I want to constantly run and send serial data every 10 seconds.
It works great but when I save it is an Application it won’t quit.
Searching around it, it seems like I need some kind of idle command but I’m out of my depth.
Any ideas how I could impliment something like that into my script so that it will run as an app?
Also, the line “tell me to quit” doesn’t work when I save it as an app, this might be part of the same problem.
Any ideas?
Thanks.
Phil
try
set port_name to do shell script "ls /dev/cu.usb*"
on error
display dialog "ERROR: Are you sure the box is plugged in?" buttons {"Quit"} default button "Quit"
tell me to quit
end try
property ytURL : "http://gdata.youtube.com/feeds/api/videos?q=v=ae_DKNwK_ms"
set tempFile to (path to temporary items as text) & (do shell script "/bin/date +%Y%m%d%H%M%S")
set a to 0
repeat while a = 0
set portRef to serialport open port_name bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
if portRef is equal to -1 then
display dialog " could not open port "
tell me to quit
end if
try
do shell script "curl " & quoted form of ytURL & " > " & quoted form of POSIX path of tempFile
on error errMsg
display dialog "ERROR: " & errMsg buttons {"Quit"} default button "Quit"
tell me to quit
end try
tell application "System Events"
set xmlData to contents of XML file tempFile
set entryTag to XML element "entry" of XML element 1 of xmlData
set theTitle to value of XML element "media:title" of XML element "media:group" of entryTag
set viewCount to value of XML attribute "viewcount" of XML element "yt:statistics" of entryTag
end tell
do shell script "rm " & quoted form of POSIX path of tempFile
set cmd to "<" & viewCount & ">"
serialport write cmd to portRef
serialport close portRef
delay 10
end repeat
try this, save the script as application with stay open option
property ytURL : "http://gdata.youtube.com/feeds/api/videos?q=v=ae_DKNwK_ms"
on run
try
set port_name to do shell script "ls /dev/cu.usb*"
on error
display dialog "ERROR: Are you sure the box is plugged in?" buttons {"Quit"} default button "Quit"
quit
end try
end run
on idle
set portRef to serialport open port_name bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
if portRef is equal to -1 then
display dialog " could not open port " buttons {"Quit"} default button "Quit"
quit
end if
set tempFile to (path to temporary items as text) & (do shell script "/bin/date +%Y%m%d%H%M%S")
try
do shell script "curl " & quoted form of ytURL & " > " & quoted form of POSIX path of tempFile
on error errMsg
display dialog "ERROR: " & errMsg buttons {"Quit"} default button "Quit"
quit
end try
tell application "System Events"
set xmlData to contents of XML file tempFile
set entryTag to XML element "entry" of XML element 1 of xmlData
set theTitle to value of XML element "media:title" of XML element "media:group" of entryTag
set viewCount to value of XML attribute "viewcount" of XML element "yt:statistics" of entryTag
end tell
do shell script "rm " & quoted form of POSIX path of tempFile
set cmd to "<" & viewCount & ">"
serialport write cmd to portRef
serialport close portRef
return 10
end idle
on quit
continue quit
end quit
When a well-behaved application receives the ‘quit’ command, it finishes what it’s doing and then tidies up before actually quitting. In the case of a script applet, what it’s doing is running the script, so a ‘quit’ command in the middle of the script has no effect until the script finishes.
Unless your applet’s been saved as “Stay open”, you should only need to stop the script and the applet will quit by itself anyway. You could use an explicit “User canceled.” error:
display dialog "ERROR: " & errMsg buttons {"Quit"} default button "Quit"
error number -128 -- Not 'quit'.
Or, since you’re displaying a dialog first, you could get the exit button to generate the error: