I have a script that I saved as an application, it is then run by cron once a week. It sends the log of my telephone switch to me per email. All this happens on my blind server, a MM.
When I run it manually it always runs fine. However when I let it be ran by cron it always hangs in the same place, and I can not find out as to why.
Maybe one of you has an idea as to why it always hangs in the same place, here (tell application “Finder”
set the name of file “gespr_dat.csv.gz” to ((“week “) & (do shell script “date +%U”) & (”.gz”))
end tell)
The script follows. I realise that getting in to the window of my phone switch and getting it to save the file would be hard to duplicate for any of you as you do not have the switch. However maybe there are hints there as to why it does not works. So I posted it too.
-- get file
tell application "Safari"
activate
delay 4
tell application "System Events"
delay 1
keystroke "http://192.168.1.190/statics/page_list.htm"
delay 2
keystroke (ASCII character 13)
delay 2
keystroke tab
delay 1
keystroke "xxx" -- Note: Username goes here if one is wanted/needed
delay 1
keystroke tab
delay 1
keystroke "xxx" -- Note: Password goes here if one is wanted/needed
delay 1
keystroke (ASCII character 13) -- Note: If ASCII character 13 doesn't work try ASCII character 10
delay 5 -- allow time to load it
repeat 23 times -- moving to the right button
key code 48 using option down
end repeat
delay 4
keystroke (ASCII character 13)
-- start the log out, close all windows, needed to prevent the browser of the server hogging the switich
delay 3
repeat 2 times
key code 13 using command down
delay 0.168
end repeat
delay 3
key code 45 using command down
delay 1
keystroke "http://192.168.1.190/statics/page_list.htm"
delay 2
keystroke (ASCII character 13)
delay 2
repeat 3 times
key code 48 using option down
delay 0.168
end repeat
delay 1
keystroke (ASCII character 13)
-- quit safari
delay 2
tell application "Safari"
quit
delay 2
tell application "System Events"
-- is it running? because if it does not quit here it hangs and hogs the phone switch
if (application process "Safari" exists) then -- kill it if it is
do shell script "killall Safari"
end if
end tell
end tell
end tell
end tell
-- rename file, here we get in to trouble when ran automatically via cron
delay 2
tell application "Finder"
set the name of file "gespr_dat.csv.gz" to (("week ") & (do shell script "date +%U") & (".gz"))
end tell
-- I need to change the name, and like to do this here, so we can store the different reports immediately and not having to change the name by hand later
-- it will now mail the file to me but that all works like a dream
I can’t make the privilge violation happen, but if it had kicked in, then you wouldn’t necessarily see anything while running your script in Script Editor.
I can on the otherhand not see your file gespr_dat.csv.gz anywhere in the script either, not that it is processed either, so I guess it is processed by something behind your UIScripting. The file may be busy while you are trying to renaming it.
This can be fixed with a loop where System Events stalls the progress until the file isn’t busy anymore.
But I still doesn’t see any other reference to that file than the file name. There must be a full path somewhere?
Understood, however in Script editor it executes flawlessly every time. No errors.
This file is created (standard format and name) by the telephone switch when, via Safari, I ask it to save it (this is what the beginning of the script does). Safari is set to save it to the desktop. Hence no reference to it in the beginning of the script.
I do not think the file is busy. When ran from cron, it simply does not get created by Safari.
Well, I have a good read for you! man curl. Curl should be able to take care of it all for you. That is without the UIScripting that is prone to break, even before you run the applet from a cron job.
You should clean up the nested tell blocks like this:
tell application "Safari" to activate
delay 4
tell application "System Events"
delay 1
keystroke "http://192.168.1.190/statics/page_list.htm"
delay 2
keystroke (ASCII character 13)
delay 2
keystroke tab
delay 1
keystroke "xxx" -- Note: Username goes here if one is wanted/needed
delay 1
keystroke tab
delay 1
keystroke "xxx" -- Note: Password goes here if one is wanted/needed
delay 1
keystroke (ASCII character 13) -- Note: If ASCII character 13 doesn't work try ASCII character 10
delay 5 -- allow time to load it
repeat 23 times -- moving to the right button
key code 48 using option down
end repeat
delay 4
keystroke (ASCII character 13)
-- start the log out, close all windows, needed to prevent the browser of the server hogging the switich
delay 3
repeat 2 times
key code 13 using command down
delay 0.168
end repeat
delay 3
key code 45 using command down
delay 1
keystroke "http://192.168.1.190/statics/page_list.htm"
delay 2
keystroke (ASCII character 13)
delay 2
repeat 3 times
key code 48 using option down
delay 0.168
end repeat
delay 1
keystroke (ASCII character 13)
end tell
-- quit safari
delay 2
tell application "Safari" to quit
delay 2
tell application "System Events" to set safariBool to exists application process "Safari"
-- is it running? because if it does not quit here it hangs and hogs the phone switch
if safariBool then -- kill it if it is
do shell script "killall Safari"
end if
I think this job is really better done with curl, as you don’t have to take any delays or such into account, as you have when UI Scripting Safari. (There are many examples of curl-usage to be found here.
Should you wish to proceed with Safari, you should look into some scripts that waits for pageload, there are several examples around here, so you can be sure that your script doesn’t commence, with the UI scripting before Safari gets a chance to react sanely. I believe that to be the culprit, since the generated file isn’t where it should be when your script tries to rename it.
Once set up properly, curl will get the data for you in one go, reliably, every time.
Well, I am not sure if you have to click the button, when you get the page via curl. And did you see the -u option, for entering the username and password?
You have also the verbose mode, for making curl really talkative, so you can figure out what is going on, while you talk to your device by it.
You should also search for curl here, and see how people use it, and have used it, for logging onto stuff, and attaining information.
About the buttons. I think, that if you are lucky, then the field of that page, will get an event that the data is submitted, without you having to click that button, effectively bypassing it. But I may very well be wrong in that. I wouldn’t give up however, before I was sure that the data was entered into the correct fields.
What you should try, is to have a debugger/inspector open in Safari, or whatever browser, and see if you can see the transaction, so that you can hopefully see the post/get event that happens when you submit the data from the form, for that is what you should use curl for, to send over, simulating the filling out of the form.
I did not see your post till just now, sometimes this forum does not send me a mail.
Have just implemented the other suggestions (the clean-up) and testing those. Next i do yours. Do not want to introduce to many new things, so I can see what works or not. Will report if it solved the problem.
Cleaning up the script by using the different solutions above did not work. Worked fine in the script editor, but using it via cron as a application created the same problem as before.
I figured out the curl suggestion, see below. I am now testing it (in cron, as it works fine as a application) and will report back.
do shell script "curl http://192.168.1.190/data_tmp/gespr_dat.csv.gzl -s -o /Users/HDname/Desktop/gespr_dat.csv.gz"
delay 2
-- rename file
tell application "Finder"
set the name of file "gespr_dat.csv.gz" to (("week ") & (do shell script "date +%U") & (".gz"))
end tell
I recommend to put the shell script before the Finder tell block and check for the existence of the file
set weekName to "week " & (do shell script "date +%U" & ".gz")
tell application "Finder"
if exists file "gespr_dat.csv.gz" then
set the name of file "gespr_dat.csv.gz" to weekName
else
log "file 'gespr_dat.csv.gz' does not exist"
end if
end tell