Using backslashes in text replacement in AppleScript - macos

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)

Related

AppleScript to append date to file

I was recently able to make a drag and drop script in Automator that allowed me to zip and name a file and then automatically apply the date (DDMMYY) but now it's defaulting to (DDMMYYYY) and I can't change it. I've googled for a solution and nothing works since this needs to be at the end of the file name.
Does anyone know what I'm doing wrong or does anyone have an actual script that can help me? Everything I've found only works if the date is at the start of the file name, not at the end (but before the extension).
You can use this AppleScript inside a Run AppleScript action, it renames the archive inserting the date before the file extension
on run {input, parameters}
set thePath to POSIX path of (item 1 of input)
set currentDate to do shell script "date +%d%m%y"
set newPath to text 1 thru -5 of thePath & "_" & currentDate & ".zip"
do shell script "mv " & quoted form of thePath & space & quoted form of newPath
return {POSIX file newPath as alias}
end run
Since you didn't provide any code I can't guess how you name your files, but the way to get DDMMYY is to use shell with the do shell script command:
tell application "Finder"
set theFile to (choose file)
set theName to name of theFile
set name of theFile to theName & "_" & (do shell script "date +%d%m%y") & ".xxx"
end tell
This doesn't get rid of the file extension of the original file. For that to work you would have to use something like this:
tell application "Finder"
set theFile to (choose file)
set theName to name of theFile
set periodIndex to offset of "." in theName
set theName to text 1 thru (periodIndex - 1) of theName
set name of theFile to theName & "_" & (do shell script "date +%d%m%y") & ".xxx"
end tell
The code responsible for removing the file extension comes from this post.
You can add punctuation how you like by putting the desired symbols before the next %. So date +%d.%m.%y is possible and improves readability.

Can't write using AppleScript/ShellScript to a file in the main library/Application Support folder

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"

using Paragraphs in Xcode in AppleScriptObjC

do shell script "mdfind kMDItemCFBundleIdentifier = com.test.sample > ~/Documents/sampleBuilds.txt"
set homeFolder to (path to home folder)
set sampleFiles to paragraphs of (read POSIX file (POSIX path of homeFolder & "/Documents/sampleBuilds.txt"))
if (count of sampleFiles) is 0 then
display notification "No Sample builds are present on the system"
else
set sampleFiles to paragraphs of (read POSIX file (POSIX path of homeFolder & "/Documents/sampleBuilds.txt"))
display dialog "You have " & (count of sampleFiles) & " build(s) of Sample on your system. Would you like to remove them all? This cannot be undone"
repeat with c in SampleFiles
do shell script "rm -rf " & (quoted form of c)
display notification "Sample build located at " & c & " has been removed from your system"
end repeat
do shell script "rm -rf ~/Documents/SampleBuilds.txt"
end if
When placing script into Xcode with correct sender I get the error:
[AppDelegate testScript:]: Can’t make current application into type file. (error -1700) on the line
set sampleFiles to paragraphs of (read POSIX file (POSIX path of homeFolder & "/Documents/sampleBuilds.txt"))
Any thoughts on how to get Xcode in AppleScript Application to accept this script?
The coercion to POSIX file is not needed which solves the issue.
set sampleFiles to paragraphs of (read (POSIX path of homeFolder & "/Documents/sampleBuilds.txt"))
Basically in AppleScriptObjC POSIX file foo doesn't work, you have to write foo as POSIX file.
Side note:
Reading the text twice is redundant. You can delete the second set sampleFiles to ... line.

Two file path types in one command?

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.

Applescript: Finder got an error: AppleEvent handler failed

I have this script set up or rename my torrent folders, however it keeps giving me that weird finder error. It works when I don't read it from a text file, but I need that so that I don't need to select the folder all the time: Please help:
set read_folder to read (POSIX path of "/.torrentcleanup-prefs.txt") as text
set autofolder to (POSIX file read_folder)
set folderlist to ("")
tell application "Finder" to set folderlist to (get name of folders of folder autofolder)
repeat with i in folderlist
set dfilepath to (POSIX path of ((autofolder & i) as text))
set dfoldername to quoted form of POSIX path of dfilepath
set dfolder to i
set dmovie to quoted form of (text 1 thru ((length of dfolder) - 7) of dfolder as text)
try
do shell script "cd " & dfoldername & "; mv ./*.mkv ../" & dmovie & ".mkv"
end try
try
do shell script "cd " & dfoldername & "; mv ./*.mp4 ../" & dmovie & ".mp4"
end try
do shell script "rm -r " & dfoldername & ""
end repeat
By the Way, the contents of "/.torrentcleanup-prefs.txt" is:
/Users/student/Desktop/FIX ME NOW/Test Folder/
I didn't look at your whole script but you're reading the file wrong in the first line. You need to do it like this. So replace your first line with these two...
set myFile to "/.torrentcleanup-prefs.txt"
set read_folder to read (POSIX file myFile)

Resources