Applescript objc 如何将移动的应用程序重新启动到新位置?

Applescript objc 如何将移动的应用程序重新启动到新位置?,applescript-objc,Applescript Objc,我试图模拟它的作用,也就是说,当用户运行应用程序时,他会检查应用程序是否从应用程序文件夹中运行。如果他没有,他会显示一个警报,要求用户复制应用程序文件夹中的应用程序,如果他单击按钮“移动到应用程序文件夹”他移动了应用程序,但我无法从新位置重新启动应用程序。我想知道怎么做,提前谢谢 on moveMyApp() set checkpath to ((path to "apps" as string) & "Lets Move") set myApp to (

我试图模拟它的作用,也就是说,当用户运行应用程序时,他会检查应用程序是否从应用程序文件夹中运行。如果他没有,他会显示一个警报,要求用户复制应用程序文件夹中的应用程序,如果他单击按钮“移动到应用程序文件夹”他移动了应用程序,但我无法从新位置重新启动应用程序。我想知道怎么做,提前谢谢

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类还做了很多其他的事情,比如获得授权和清除隔离标志,但是您可以在启动NSTask后告诉应用程序终止,该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
文件夹