4

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?

0

3 Answers 3

2

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 
/
0
1

@user3439894 has already explained how to solve the problem; I'd like to take a stab at explaining why the confusion exists. It goes back to the fact that the Macintosh OS wasn't always based on unix (/POSIX). Back before OS X (which is unix-based), Mac OS was a completely different OS with its own file path notation. When OS X came out it had to support both the unix file access APIs (which used unix path notation) and the traditional Mac OS APIs (which used traditional Mac OS path notation). Over the years, OS X (/macOS) has deprecated some of the traditional Mac OS APIs and switched more and more towards pure unix path notation. But AppleScript is an exception: it predates OS X, so it natively uses the old traditional Mac OS notation.

Here's a summary of the differences:

  • Unix (/POSIX) paths use "/" as delimiters (and allow ":" within filenames), while Mac OS paths use ":" as delimiters (and allow "/" within filenames).
  • Unix paths are absolute if they start with the delimiter ("/"); Mac OS paths are absolute if they do not start with the delimiter (":").
  • Absolute Mac OS paths start with the volume name, while absolute unix paths start at the top of the root volume, and other volumes appear as folders somewhere under it.

Here are some examples:

Mac OS: Macintosh HD:Users:vader:someapp.app:Contents:Resources:Scripts:foobar.txt
Unix: /Users/vader/someapp.app/Contents/Resources/Scripts/foobar.txt

Mac OS: Other disk:daily notes:7/19/2017 meeting.rtf
Unix: /Volumes/Other disk/daily notes/7:19:2017 meeting.rtf

Mac OS: :Documents:file.txt
Unix: Documents/file.txt

In AppleScript, you can use POSIX path of and as alias to convert between these.

0
-2

Your original script specified "cp Macintosh HD ...", but use /Volumes/Macintosh\ HD ... instead. Also, it's best to put a backslash before any blank, as I did above before HD. That would be true for 'Application\ Support' as well

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .