AppleScript do shell will not work on Application Support directory

applescriptbashrootterminal

I've spent the day doing several researches and attempts on how to copy a file that is located inside an AppleScript app to the root Application Support directory (hdd -> Library -> Application Support). Originally I thought it was an issue with calling do shell script and administrator privileges but for some reason if I have a file located in an AppleScript app I try to call that file to be added to Application Support, for example:

set foo to "Macintosh HD/Users/vader/someapp.app/Contents/Resources/Scripts/foobar.txt"
set bar to "Macintosh HD/Library/Application Support/"

do shell script "cp " & (quoted form of foo) & space & (quoted form of bar) with administrator privileges

I've taken this down to the basic form when it comes to the folder path and file path but when I run the above I get an error message of:

cp: Macintosh
HD/Users/vader/someapp.app/Contents/Resources/Scripts/foobar.txt: No
such file or directory

I can return true that the file exists in the app with:

set foobar to POSIX path of (path to resource "foobar.txt" in directory "Scripts")
if not (exists foobar) then
    return false
else
    return true
end if

I've tried this with:

tell application "Finder" to duplicate file (foo) to folder (bar)

but that didn't work either.

Research when I tried to remove the file after manually adding it (yes I have admin permissions on my box):

I've researched several areas to see how to implement the prompting of permissions using administrator privileges but with no luck:

Here are the following attempts I've tried:

AppleScript System Events:

tell application "System Events" to delete file appWithPath

do shell attempts:

attempt 1

do shell script "rm -f " & appNamewithQuotedFormPath with administrator privileges

attempt 2

do shell script "find " & quotedPathToDirectory & " -name \"" & applicationName & "\" -type f -delete" with administrator privileges

It still wouldn't work and I checked with:

do shell script "echo " & appNamewithQuotedFormPath with administrator privileges

After digging into Script Editor I found I was getting -10004 error so I researched:

so I moved the add and removal of the file in Application Support to a handler that didn't have a try block but still nothing.

Thinking it might be an issue with the used privileges I tried all of the following parameters used with Scripting Additions:

enter image description here

In an AppleScript app how can I add and remove a file that exists in the app to the root Application Support directory?


EDIT:

After further testing I removed the usage of Macintosh HD and the do shell script would work which I do not understand why that would cause the script to fail. Why would adding Macintosh HD to the path cause the script to error out?

Best Answer

If you are hardcoding, without testing if the file exists, then use the following example:

set foo to quoted form of "/Users/vader/someapp.app/Contents/Resources/Scripts/foobar.txt"
set bar to quoted form of "/Library/Application Support/"
try
    do shell script "cp " & foo & space & bar with administrator privileges
end try

If you are calling it from the script within the application bundle, then use the following example:

on run
    try
        set foo to POSIX path of (path to me as string) & "Contents/Resources/Scripts/foobar.txt"
        set bar to quoted form of "/Library/Application Support/"

        tell application "System Events"
            if exists file foo then
                tell current application
                    do shell script "cp " & quoted form of foo & space & bar with administrator privileges
                end tell
            end if
        end tell
    on error eStr number eNum
        display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
        return
    end try
end run

Note: When working with the do shell script command, POSIX pathnames must be used, not aliases. Macintosh HD is not valid in a POSIX path, just in an alias. In a POSIX path of the startup disk, everything starts in the root of the volume: /

Example:

tell application "Finder" to get name of startup disk as alias
tell application "Finder" to get POSIX path of (name of startup disk as alias)

Returns:

Macintosh HD 
/