applescript-objc

How to move App to Applications folder when running it?


What I'm trying to do is that when the user runs the App from outside the Applications folder, a display alert appears asking if he wants to move the App to the Applications folder if the App is not already there. I tried the following:

tell application (path to frontmost application as text)
        set myApp to path of document 1
        set folderApplications to "/Applications" as POSIX file
        set anyApp to "/Applications/My.app" as POSIX file
        tell application "Finder"
            if exists anyApp then
                display alert "There is already an App with the same name in Applications Folder.
                return
                else
                tell application "System Events"
                    move myApp to folderApplications
                end tell
            end if
        end tell
    end tell

ERROR: Can’t get «class FTPc» of document 1. (error -1728)


Solution

  • Something like this may work for you. The code must first be saved as an application named "myApp.app", for this to function correctly.

    property myName : "myApp.app"
    
    set folderApplications to path to applications folder as text
    set pathToMe to path to me as text
    
    tell application "Finder"
        set appExists to exists of alias (folderApplications & myName)
    end tell
    
    if not appExists then
        beep 3
        set moveApp to button returned of (display dialog ¬
            "Would You Like To Move This App To The Applications Folder?" buttons {"No", "Yes"} ¬
            default button 2 ¬
            with title "Move This App?")
    
        if moveApp is "Yes" then
            tell application "Finder"
                move alias pathToMe ¬
                    to alias folderApplications
            end tell
        end if
    
    end if
    
    -- the rest of your code