如何将移动的应用程序重新启动到新位置?
How to relaunch the moved application to a new location?
我正在尝试模拟 Lets Move 所做的事情,也就是说,当用户运行应用程序时,他检查应用程序是否在应用程序文件夹中 运行 如果他不是,他会显示一条警告,要求用户复制应用程序文件夹的应用程序,如果他单击按钮 "Move to Applications Folder",他会移动该应用程序,但我无法从新位置重新启动该应用程序。我想知道如何做到这一点,在此先感谢。
on moveMyApp()
set checkpath to ((path to "apps" as string) & "Lets Move")
set myApp to ((path to current application as text))
tell application "Finder"
if exists file checkpath then
return
else
tell current application
display alert "Move to Applications Folder?" buttons {"Not Move", "Move to Applications Folder"} default button 2
set response to button returned of the result
if response is "Move to Applications Folder" then
do shell script "mv " & quoted form of POSIX path of myApp & space & "/Applications"
end if
end tell
tell application "Lets Move" to quit
delay 2
end if
end tell
tell application "Lets Move" to activate
end moveMyApp
你正在改编的 Objective-C class 做了很多其他事情,比如获得授权和清除隔离标志,但你可以告诉应用程序在启动后终止NSTask 在重新启动应用程序之前稍等片刻。例如,quit
和 activate
语句可以替换为对执行重新启动任务的处理程序的单个调用:
on moveMyApp()
set checkpath to ((path to "apps" as string) & "Lets Move.app")
set myApp to ((path to current application as text))
tell application "Finder" to set moved to exists file checkpath
if not moved then
display alert "Move to Applications Folder?" buttons {"Do Not Move", "Move to Applications Folder"} default button 2
set response to button returned of the result
if response is "Move to Applications Folder" then
do shell script "mv " & quoted form of POSIX path of myApp & space & "/Applications"
relaunch(checkpath)
end if
end if
end moveMyApp
on relaunch(appPath)
tell current application's NSTask's alloc()'s init()
its setLaunchPath:"/bin/sh"
its setArguments:{"-c", "sleep 1.0; open -a " & quoted form of "Lets Move"}
its |launch|()
end tell
tell current application's NSApp to terminate:me
end relaunch
请注意,虽然我稍微清理了您的处理程序,但没有完成任何错误处理(计时问题、移动应用程序失败等)。使用 Mojave 和 Xcode 10 进行测试,将应用程序移动到用户 ~/Applications
文件夹。
我正在尝试模拟 Lets Move 所做的事情,也就是说,当用户运行应用程序时,他检查应用程序是否在应用程序文件夹中 运行 如果他不是,他会显示一条警告,要求用户复制应用程序文件夹的应用程序,如果他单击按钮 "Move to Applications Folder",他会移动该应用程序,但我无法从新位置重新启动该应用程序。我想知道如何做到这一点,在此先感谢。
on moveMyApp()
set checkpath to ((path to "apps" as string) & "Lets Move")
set myApp to ((path to current application as text))
tell application "Finder"
if exists file checkpath then
return
else
tell current application
display alert "Move to Applications Folder?" buttons {"Not Move", "Move to Applications Folder"} default button 2
set response to button returned of the result
if response is "Move to Applications Folder" then
do shell script "mv " & quoted form of POSIX path of myApp & space & "/Applications"
end if
end tell
tell application "Lets Move" to quit
delay 2
end if
end tell
tell application "Lets Move" to activate
end moveMyApp
你正在改编的 Objective-C class 做了很多其他事情,比如获得授权和清除隔离标志,但你可以告诉应用程序在启动后终止NSTask 在重新启动应用程序之前稍等片刻。例如,quit
和 activate
语句可以替换为对执行重新启动任务的处理程序的单个调用:
on moveMyApp()
set checkpath to ((path to "apps" as string) & "Lets Move.app")
set myApp to ((path to current application as text))
tell application "Finder" to set moved to exists file checkpath
if not moved then
display alert "Move to Applications Folder?" buttons {"Do Not Move", "Move to Applications Folder"} default button 2
set response to button returned of the result
if response is "Move to Applications Folder" then
do shell script "mv " & quoted form of POSIX path of myApp & space & "/Applications"
relaunch(checkpath)
end if
end if
end moveMyApp
on relaunch(appPath)
tell current application's NSTask's alloc()'s init()
its setLaunchPath:"/bin/sh"
its setArguments:{"-c", "sleep 1.0; open -a " & quoted form of "Lets Move"}
its |launch|()
end tell
tell current application's NSApp to terminate:me
end relaunch
请注意,虽然我稍微清理了您的处理程序,但没有完成任何错误处理(计时问题、移动应用程序失败等)。使用 Mojave 和 Xcode 10 进行测试,将应用程序移动到用户 ~/Applications
文件夹。