POSIX Path Variable of Containing Folder | Applescript - macos

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

Related

Using AppleScript to Select a File

I'm trying to get AppleScript to select a file, but I'm getting an error when I execute the script.
Here's the code
tell application "System Events"
set a to "/Users/me/files/"
set fileName to "myFile.jpg"
set thePath to POSIX path of a
tell application "Finder"
set selection to fileName of thePath
end tell
keystroke "c" using command down
end tell
I'm getting an error "Can’t get POSIX path of "/Users/me/files/"
Essentially, what I'm trying to do is find a way to select a file so that I can copy it for later. But I want to copy the actual file, not the path of the file. The idea is to create a service that copies the file so that I can paste it into another application easily.
If there's a better way to do this, then please let me know
These following 2 lines of code would copy your file to the clipboard. This would only work with a single file. Not multiple items.
activate
set the clipboard to POSIX file (POSIX path of (choose file))

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

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"
^^ ^^

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)

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

POSIX File Error - Extraneous double quotes?

This applescript works
set myFile to (POSIX path "/Users/fred/Documents/data.dat")
This applescript doesn't work
set myFileName to "/Users/fred/Documents/data.dat"
set myFile to (POSIX path myFileName)
It fails with the error
get POSIX file "/Users/fred/Documents/data.dat"
--> error number -1728
Result:
error "iTunes got an error: Can’t get POSIX file \"/Users/fred/Documents/data.dat\"." number -1728 from file "Macintosh HD:Users:drew:Desktop:Music:DIY:DIY-01.mp3"
It looks as if when using the variable, POSIX path is including the double quotes as explicit characters in the file name. What am I doing wrong?
The script below reproduces the problem.
tell application "Finder"
set newFileName to "/Users"
set newFile to POSIX file newFileName
end tell
Thanks
OK - I've found out what I should be doing.
The script below works - you just need to coerce the variable rather than pass it to POSIX file
tell application "Finder"
set newFileName to "/Users"
set newFile to (newFileName as POSIX file)
end tell
Result...
file "Macintosh HD:Users"
Thanks for your assistance.
Andrew
"/Users/fred/Documents/data.dat" Is already a posix path
tell application "Finder" to open POSIX file "/Users/fred/Documents/data.dat"
or
tell application "System Events" to open "/Users/fred/Documents/data.dat"
Here is an example for iTunes:
tell application "Finder" to set myFile to (POSIX file "/Users/John/Desktop/08 5150.mp3")
tell application "iTunes" to set resultTrack to add myFile to playlist "test"

Resources