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
其中LocateMe是一个bash脚本,可以使用-f
命令获取用户的纬度和经度以及其他统计信息。
现在,首次运行时,程序会要求获取用户位置的权限。如果用户按“OK”,那么一切都很好。但如果用户按下“取消”,我们就会遇到问题。
正如我在SuperUser上对deleting applications from Location Services所描述的那样,LocateMe似乎继续在后台运行,直到重新启用位置服务。与此同时,我的Applescript只是挂起,直到bash脚本完成加载,或直到我强制退出应用程序(或脚本编辑器,无论我在运行它)。显然,这不是一种理想的行为。
更糟糕的是,一旦申请人要求获得位置服务许可,它就再也不会要求了;用户必须手动转到“系统偏好设置”并自行打勾(或get a script to do it for him)。这意味着在这个脚本的第二次运行中,用户绝对无法知道脚本挂起的原因。
我第一次尝试解决这个问题是将LocateMe填入try
声明中,但根据我上面的结论,这不会做任何事情;由于LocateMe继续运行直到它可以访问用户的位置,因此Applescript永远不会到达on error
行。
我想知道是否有办法从某种语句中调用LocateMe,无论是Applescript还是bash脚本,还是可以在Applescript中运行的其他语言,它设置了一个时间限制;如果未达到时间限制,LocateMe将被终止,Applescript将向用户显示错误消息,说明必须打开位置服务。
答案 0 :(得分:1)
这可能是How to introduce timeout for shell scripting?
的副本如果您有超时可用,只需运行timeout -k 10 Contents/Resources/LocateMe
(其中10是超时前的秒数。
否则要么直接调用expect命令,用Contents/Resources/LocateMe
替换$ command,要么将该函数复制到.bashrc中,以便在bash中可以使用函数超时,并调用它。
期望的例子是定义一个bash函数 - 当你用参数调用函数时,它们被放入名为$1
,$2
等的变量中。所以对于timeout 10 foo
,{{1} }将是$1
而10
将是$2
。如果您将其“原样”复制到foo
中,则可以使用与上面建议的.bashrc
命令相同的方式使用它。或者,只需将函数中的expect行复制到脚本中,并用硬编码值(时间,命令等)替换所有变量。