Reputation: 169
on run{}
set myPath to path to me as text
set x to the length of myPath
set myPath to characters 13 thru x of myPath as string --Removes "Macintosh HD:" from the front of the file path
set myPath to my fileAdapt(myPath)
set lat to do shell script myPath & " -f \"{LAT}\"" --See comments below the script
set lon to do shell script myPath & " -f \"{LON}\""
end run
on fileAdapt(myPath)
set x to the length of myPath
set myPath1 to ""
repeat with i from 1 to x
if character i of myPath is ":" then
set myPath1 to myPath1 & "/"
else if character i of myPath is " " then
set myPath1 to myPath1 & "\\ "
else
set myPath1 to myPath1 & (character i of myPath)
end if
end repeat
set myPath1 to myPath1 & "Contents/Resources/LocateMe" as string --See comments below the script
return myPath1
end fileAdapt
where LocateMe is a bash script that can get a user's latitude and longitude, among other statistics, using the -f
command.
Now, on first run, the program asks for permission to get the user's location. If the user presses "OK," then all is good. But if the user presses "Cancel," we've got issues.
As I describe over on SuperUser regarding deleting applications from Location Services, LocateMe seems to continue running in the background until Location Services are turned back on. In the meantime, my Applescript just hangs until the bash script finishes loading, or until I force-quit the application (or Script Editor, whatever I'm running it in). Obviously, this is not a desirable behavior.
To make matters worse, once the application has asked for Location Services permission, it never asks again; the user has to manually go to System Preferences and tick the box themselves (or get a script to do it for him). What this means is that on a second run of this script, the user would have absolutely no way of knowing why the script is hanging.
My first attempt at dealing with this was to stuff LocateMe inside a try
statement, but, as per my conclusion above, that wouldn't do anything; since LocateMe continues running until it gets access to the user's location, the Applescript never reaches the on error
line.
I would like to know if there is a way to call LocateMe from within some sort of statement, be it an Applescript or a bash script or some other language that can be run from within an Applescript, that sets a time limit; if the time limit is not reached, LocateMe will be terminated, and the Applescript will display an error message to the user, explaining that location services must be turned on.
Upvotes: 0
Views: 331
Reputation: 11060
This is a possible duplicate of How to introduce timeout for shell scripting?
If you have timeout available, simply run timeout -k 10 Contents/Resources/LocateMe
(where 10 is the number of seconds before timeout.
Otherwise either call that expect command directly, replacing $command with Contents/Resources/LocateMe
, or copy that function into your .bashrc so that the function timeout is available in bash, and call that.
That expect example is defining a bash function - when you call functions with arguments, they get put into variables called $1
, $2
etc. So for timeout 10 foo
, $1
would be 10
and $2
would be foo
. If you copy it 'as-is' into your .bashrc
you can use it in the same way as the timeout
command suggested above. Alternatively, just copy the expect line out of the function into your script, and replace all the variables with hard-coded values (time, command etc).
Upvotes: 1