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:
- Can't delete directory via AppleScript
- AppleScript: match and delete files in folder
- Automatically delete temporary ~/Library/Application Support folders
- Delete folders if exist and delete
- Applescript oe shellscript to delete config file?
- Delete files with AppleScript to recycle bin or permanently
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
:
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:
If you are calling it from the script within the application bundle, then use the following example:
Note: When working with the
do shell script
command, POSIX pathnames must be used, not aliases.Macintosh HD
is not valid in aPOSIX path
, just in an alias. In aPOSIX path
of thestartup disk
, everything starts in the root of the volume:/
Example:
Returns: