I did the following and it works fine, I added my Command Line Tool to the Resource Folder of my project and did the following below:
set myPath to POSIX path of (path to resource "MyComandLineTool")
do shell script "cp " & quoted form of POSIX path of myPath & space & "/private/tmp" with administrator privileges
set cmd to "/private/tmp/myPath -r"
do shell script " " & cmd with administrator privileges
I wanted to know how not to pass it to /private/tmp, just run it from within the Resources folder of my project.
It's quite similar, just omit the copying part
set myPath to quoted form of (POSIX path of (path to resource "MyComandLineTool"))
do shell script myPath & " -r" with administrator privileges
Note: quoted form of is always good practice as the path could contain space characters
Related
I'm trying to write some text into an xml file situated in a subfolder of the main library/Application Support folder using a shell script
do shell script "echo '" & theText & "' > " & thePath.
Without a password, I get a "sh: /Library/Application: Permission denied" which is perfectly logical.
Adding user name and password, as shown in the code below, I no longer get any error, but no text is written to the file.
If I put a wrong user name or password, it gives me "The user name and password were incorrect", which shows that the password is indeed being taken into account.
Am I trying to do something impossible, or is my code missing something ?
set thePath to POSIX path of ("/Library/Application Support/MyApp/Stuff/test.xml" as text)
set theText to "<ProductSpecific><Visibility type=\"Number\">3</Visibility></ProductSpecific>"
set theScript to "echo '" & theText & "' > " & thePath
do shell script theScript user name "myshortusername" password "mypassword" with administrator privileges
I should get theText written to /Library/Application Support/MyApp/Stuff/test.xml
but nothing is written although I don't get an error message either ! Oh, and if I move the file to the desktop and change the path, it all works fine !!
The reason of the error is the space character in Application Support. You have to escape the entire path.
It's highly recommended to use always quoted form of
set theScript to "echo " & quoted form of theText & " > " & quoted form of thePath
Side note:
The string path is already a POSIX path (and it's already text) so you can omit both
set thePath to "/Library/Application Support/MyApp/Stuff/test.xml"
I'm currently trying to create an AppleScript Application which will copy a folder from its "Contents" folder to the user's desktop. The code I have at the moment is:
set theFile to path to me as string
set hfsme to theFile & "Contents:Folder"
set posixPath to POSIX path of hfsme
set contentfolder to posixPath
set AppleScript's text item delimiters to " "
set textItem to text items of contentfolder
set AppleScript's text item delimiters to "\\ "
set contentfolder to textItem as string
set AppleScript's text item delimiters to {""}
do shell script "cp -a " & contentfolder & " $HOME/Desktop"
However, I receive the following error upon run:
"cp: /Users/myusername/Desktop/Test Application.app/Contents/Folder: No such file or directory"
I think this is because I expect the copy location to be:
/Users/myusername/Desktop/Test\ Application.app/Contents/Folder
But the script fails to add the backslash before the space which is what the shell command requires.
My question is: how do I replace the spaces in the variable with "\ " ?
P.S. Before suggesting simply renaming the application a name which doesn't have a space, it is very important for me to have an application name with a space.
I have also tried using "duplicate" command with AppleScript but wasn't able to get this to work but I am open to suggestions!
(Please bear in mind that I'd like this script to work on other people's computers as well meaning that I cannot assume I already know the user's username.
Thank you in advance for any help!
Kind regards, Tom
Just add quoted form of, it handles the quotation in a very smart (and reliable) way.
set theFile to POSIX path of (path to me)
set contentfolder to theFile & "Contents/Folder"
do shell script "cp -a " & quoted form of contentfolder & " $HOME/Desktop"
If do shell script does not support $HOME write
do shell script "cp -a " & quoted form of contentfolder & space & quoted from of POSIX path of (path to desktop)
I have been struggling to make a basic program that copies selected files to a predetermined location. However it always ends up with the command having two different path types. is there any way that I can bypass this as it is bugging me and i have finished every other aspect of the program.
set targetFolder to (POSIX path of (path to home folder)) & "Library/Application Support/..." as POSIX file
set filepath to POSIX path of (choose file with prompt "Chose your file")
delay
do shell script "cp " & filepath & space & targetFolder
delay
display dialog "Your file has been moved!"
It's exactly the same as your previous question: Didn't you read my answer? You have to use quoted POSIX paths.
set applicationSupportFolder to POSIX path of (path to application support folder from user domain)
set filepath to POSIX path of (choose file with prompt "Chose your file")
do shell script "cp " & quoted form of filepath & space & quoted form of applicationSupportFolder
And you don't need any delays.
I have been attempting to make a simple application that copies a selected file to a predetermined directory, only the variable does not work when set in a shell script. I was wondering if there is a way to phrase the copy or move command to allow it to take effect. The script is below.
set filepath to POSIX path of (choose file with prompt "Chose your file")
delay
do shell script "cp " & filepath & " ~/Library/Application Support/"
It does not work because space characters in paths must be escaped, the most reliable way is to use quoted form of.
path to application support folder from user domain is the relative path to the current user's application support folder.
set applicationSupportFolder to POSIX path of (path to application support folder from user domain)
set filepath to POSIX path of (choose file with prompt "Chose your file")
do shell script "cp " & quoted form of filepath & space & quoted form of applicationSupportFolder
set theAddresses to {"Address, Name, Counter" & return & "A#b.com, A, 1"}
set theFile to (path to desktop folder as text) & "test.csv"
do shell script "echo " & quoted form of (theAddresses as text) & " > " & quoted form of theFile
delay 1
do shell script "open " & quoted form of theFile
I get permissions error:
error "sh: Alwnick:Users:aleith1:Desktop:test.csv: Permission denied"
I tried to replicate this as a line command in bash terminal but I can't write string literal into a file with the " > " command. Nor can I find a manual for ">" fo syntax for literals. Yet echo seems to use a literal okay, despite the permissions issue.
Where should I change the permissions, in the shell script or manually in OSX Finder or in bash? What permissions should the Desktop Folder have? I tried to invoke "sudo echo…" but no gain.
You are trying to write to
Alwnick:Users:aleith...blah...blah...something
That is the Applescript version of the filename, and it is unintelligible to the shell and Unix command line utilities - basically it uses colons in places of slashes.
You need to use the "POSIX form" of the path, see here