POSIX File Error - Extraneous double quotes? - macos

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"

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

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 Error - Can't Get every file of ... folder?

I have a script that someone else wrote. It works on his computer but not mine.
Originally it said "as alias", but I kept getting an error message as follows:
"Can’t make file "Macintosh HD:Users:williamsato:Desktop:Photobooth:CurrentSession" into type alias."
So I changed alias to string, following a suggestion made in another post.
Now I don't get that error message, but I do get a new one as shown below:
"Can’t get every file of "Macintosh HD:Users:williamsato:Desktop:PhotoBooth:CurrentSession"."
And it highlights the line "move files of SourceFolder to DestinationFolder with replacing"
Not sure what is going wrong.
on run {input, parameters}
set SourceFolder to POSIX file "/Users:/williamsato/Desktop/Photobooth/CurrentSession" as string
set DestinationFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/PreviousSessions" as string
tell application "Finder"
move files of SourceFolder to DestinationFolder with replacing
end tell
(* Clear Large Type *)
tell application "System Events" to keystroke "a" using command down
end run
Here are three different way of forming the SourceFolder and DestinationFolder variables and moving the files:
set SourceFolder to (path to desktop folder as string) & "Photobooth:CurrentSession:" as alias
set DestinationFolder to (path to desktop folder as string) & "Photobooth:PreviousSessions:" as alias
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
set SourceFolder to alias "Macintosh HD:Users:williamsato:Desktop:Photobooth:CurrentSession:"
set DestinationFolder to alias "Macintosh HD:Users:williamsato:Desktop:Photobooth:PreviousSessions:"
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
set SourceFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/CurrentSession" as alias
set DestinationFolder to POSIX file "/Users/williamsato/Desktop/Photobooth/PreviousSessions" as alias
tell application "Finder" to move files of SourceFolder to DestinationFolder with replacing
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

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 Variable issue

I am having issues with this script when run it errors out
tell application "Finder"
set music_file to some file of the folder "Macintosh HD:ringtones"
end tell
set volume output volume 30
do shell script "afplay '/Volumes/Macintosh HD/ringtones/'" & music_file
this is the error:
do shell script "afplay '/Volumes/Macintosh HD/ringtones/'Macintosh HD:ringtones:Zen_ag_NARITA_HI_long_1.mp3"
--> error "may only specify one file to play
so I attempted to use a posix command such as:
set conver to POSIX file of music_file
and that errors out
What I cannot figure out is why the music_file is getting the whole HFS path in it when I tell it to just get the file and how to correct for it.
Try:
set myFolder to "/Users/me/Desktop/test"
tell application "System Events" to set music_file to POSIX path of (some file of folder myFolder)
set volume output volume 30
do shell script "afplay " & quoted form of music_file
AppleScript is weird. music_file is of type document file as defined by the Finder app, instead of being some other kind of file object. If you convert it to an alias with music_file as alias, then you can get the POSIX path of it which is what’s needed on the command line. Finally you need to wrap another set of ' characters around the filename in case it includes spaces. Note that this will break if the filename itself contains ' characters, as in Crazy 'Cause I Believe.mp3.
tell application "Finder"
set music_file to some file of the folder "ref:Users:andrew:Music:iTunes:iTunes Media:Ringtones"
end tell
set volume output volume 30
do shell script "afplay '" & (POSIX path of (music_file as alias)) & "'"

Resources