applescript-objc

How to relaunch the moved application to a new location?


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

Solution

  • 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
    

    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.