Applescript — How can I address empty spaces on a folder path? - macos

I am having trouble executing an AppleScript using a folder path that contains empty spaces. How can I solve this issue? Thank you in advance.
tell application "Finder"
set myWin to window 1
set thePath to (quoted form of POSIX path of (target of myWin as alias))
tell application "Terminal"
activate
tell window 1
do script "/Users/johndoe/Company Dropbox/John Doe/Folder " & thePath
end tell
end tell
end tell

#red_menace solution works; However I found another option. You can add \\ to address empty spaces on a folder path.
Example:
"/Users/johndoe/Company\\ Dropbox/John\\ Doe/Folder"
^^ ^^

Related

POSIX Path Variable of Containing Folder | Applescript

I am requiring this script to store two variables.
checksum_path - The selected file POSIX path
checksum_parent_path - POSIX path of the selected file ($checksum_path)
I am having to implement the second variable (checksum_parent_path) to the script below.
checksum_path=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
try
set FileName to POSIX path of (choose file with prompt "Please choose checksum file:" of type {"SUM"})
on error
set FileName to "!ERROR!"
end try
end tell
EOD)
Any help would be appreciated, thank you !
Adrian
This should do it unless I'm misunderstanding your question.
tell application "Finder"
set theFile to choose file
set checksum_path to POSIX path of theFile
set checksum_parent_path to POSIX path of (container of theFile as alias)
end tell

Reveal in finder does not work with concatenated string

Why does this work:
tell application "Finder"
activate
reveal POSIX file ("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/file.png")
end tell
...but not this
tell application "Finder"
activate
reveal POSIX file ("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/" & "file.png")
end tell
And how do I get it to work if I want to join a path (string) with a variable (string)?
System Events handles POSIX paths a lot better, but this is just another one of those AppleScript oddities. POSIX file will work outside of a Finder tell statement:
set x to POSIX file (pathVariable & otherPathVariable)
tell application "Finder"
activate
reveal x
end tell
but within a Finder tell statement you need to use it as a coercion:
tell application "Finder"
activate
reveal (pathVariable & otherPathVariable) as POSIX file
end tell
I recommend to use relative HFS paths. The first line points to the library folder of the current user.
set libraryFolder to path to library folder from user domain as text
tell application "Finder"
reveal file (libraryFolder & "com~apple~CloudDocs:MyFolder:" & "file.png")
end tell
try this to combine strings
reveal POSIX file (("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/" & "file.png") as text)

Error when replacing characters applescript

I am trying to make a script that executes a runnable Jar file in the form of an applescript application. I'm not very experienced in applescript, so I mostly went off of online snippets. The first problem I ran into was that the directory automatically formats as using a ":" instead of a slash (ex. "usr:bin" instead of "usr/bin") so I found something that's supposed to replace the : with a /. This, however, produces an error every time.
I've found little on how to fix my error and the stuff I found was very specific to those cases and didn't make much sense to me.
This is my code:
tell application "Finder"
set current_path to container of (path to me) as alias
end tell
set AppleScript's text item delimiters to ":"
set currPath to text items of current_path
set AppleScript's text item delimiters to "/"
set current_path to currPath as string
set AppleScript's text item delimiters to {""}
currPath
tell application "Terminal"
activate
set currentTab to do script ("cd " & current_path)
set currentTab to do script ("java -jar Inoisulcnoc Pre-Alpha 2")
end tell
The error I get every time says:
error "Can’t get every text item of alias \"Macintosh HD:Users:knotsnappy:Desktop:Inoisulcnoc Pre-Alpha 2:\"." number -1728 from every text item of alias "Macintosh HD:Users:knotsnappy:Desktop:Inoisulcnoc Pre-Alpha 2:"
How should I be able to fix this?
You'll want to change the standard path into a POSIX path.
tell application "Finder"
set current_path to (POSIX path of (container of (path to me) as alias))
end tell
tell application "Terminal"
activate
set currentTab to do script ("cd " & current_path)
set currentTab to do script ("java -jar Inoisulcnoc Pre-Alpha 2")
end tell
The delimiters, etc. are unneeded once you have the correct syntax.

Applescript rename file in current folder

I want to change a filename in the folder which is not always the same, it depends on where the applescript is stored (same folder as the file to change).
I made this script with a dialog to check the path, that works fine but after but I get an error (-1700, Can't change "test" into an integer. Why, and how do I fix this?
tell application "Finder"
set thePath to POSIX path of ((path to me as string) & "::")
display dialog thePath buttons {"Clipboard", "OK"} default button 2
if the button returned of the result is "Clipboard" then
set the clipboard to thePath
end if
set name of document file "test" of thePath to "test_OLD"
end tell
If you are using Finder, which you only need for the set name statement, you need to coerce thePath from a posix path to a hfs path.
You can also remove the entire Finder block and use:
tell application "System Events" to set name of file (thePath & "test") to "test2"
Could this be an easier way syntax wise?
tell application "System Events"
set name of file "/Users/Firebird/Documents/test2.jpg" to "/Users/Firebird/Documents/11.jpg"
end tell

Applescript path to application using variable

If I have an applescript snippet such as this
tell application "Finder"
set thePath to (POSIX path of (path to application "MyApp"))
end tell
it will return to me
"/Applications/MyApp.app"
Now, what I can't seem to figure out is how to instead specify "MyApp" via a variable rather than the literal.
My applescript reads in some XML values, one of them being the name of the application I'm interesting in. I've tried this:
tell application "Finder"
set thePath to (POSIX path of (path to application someVariable))
end tell
but this simply tells me the error
"Finder got an error: Can't make application "MyApp" into type constant."
Any ideas how I can do this?
The answer (or at least one answer) is:
set theApp to "MyApp"
set pathToTarget to POSIX path of (path to application theApp)
Since path to application is a part of Standard Additions, the Finder is not needed.
Thanks to Stephan K on MacScripter for setting me straight on this.

Resources