如何将移动的应用程序重新启动到新位置? [英] How to relaunch the moved application to a new location?

查看:231
本文介绍了如何将移动的应用程序重新启动到新位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟移动 的行为,即当用户运行他检查应用程序是否在应用程序文件夹中运行,如果不是,则显示警告,要求用户复制应用程序的应用程序文件夹,如果他单击移动到应用程序文件夹按钮,则移动应用程序,但无法从新位置重新启动该应用程序。我想知道如何做到这一点,在此先感谢。

I'm trying to simulate what Lets Move does, that is, when the user runs the application he checks to see if the application is running from within the Applications folder if he is not, he displays an alert asking the user to copy the application for applications folder, if he clicks on the button "Move to Applications Folder" he moves the application but I can't relaunch the application from the new location. I would like to know how to do this, thanks in advance.

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 语句:

The Objective-C class you are adapting this from does a bunch of other things, such as getting authorization and clearing the quarantine flag, but you can just tell the app to terminate after launching an NSTask that waits a bit before relaunching the app. For example, the quit and activate statements can be replaced with a single call to a handler that performs the relaunch task:

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 文件夹中。

Note that although I cleaned up your handler a little, no error handling is done (timing issues, failure to move the app, etc). Tested with Mojave and Xcode 10, moving the app to the user ~/Applications folder.

这篇关于如何将移动的应用程序重新启动到新位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆