AppleScript: File that exists not found - applescript

My applescript is telling me that the file I'm trying to get is not found. But it's right here, I can open it using the exact same path in the terminal.
Here is a simplified version of the code that gives the same error:
set theFile to "~/Desktop/MyFile.csv" as alias
set theContent to read theFile as «class utf8»

Your path string is incorrect for applescript. It's expecting an HFS path like this...
Path:to:my:file:MyFile.csv, not a POSIX path.
I would recommend doing something like this...
set theFile to ((path to desktop folder as string) & "MyFile.csv") as alias

Related

Applescript can't set name of file

So I'm trying to rename a file using Applescript. I searched it up and apparently the command is set name of file theFile to theString. I tried it and it didn't work. I got the error -10006, which (according to https://www.osstatus.com) is either errAEWriteDenied, errOSACantAssign or telCAUnavail, but I think it's probably the first one. Before you ask, I am the administrator of my machine and path to desktop links to the own Desktop to which I 100% have access. I don't know if that matters (it really shouldn't), but my Desktop is stored in my iCloud.
set thePath to the POSIX path of (path to desktop)
set theName to "hello world.txt"
set theFile to thePath & theName
set the name of file theFile to "hello.txt"
Only the Finder or System Events is able to set the name. In Finder it’s pretty easy because the desktop of the current user is the root folder.
tell application "Finder"
set theFile to "hello world.txt"
set theName to "hello"
set the name of file theFile to theName & ".txt"
end tell
By the way, the Finder doesn’t know about POSIX paths, it prefers HFS paths (colon separated)

Apple Script: Copy file (being selected from prompt) to another location

I am struggling with my Apple Script. I am asking to select a file and the selected file needs to be copied to another location. I am new to Apple Scripting, so probably made some mistake. I tried different versions with "copy" instead of "duplicate" or "alias" instead of "file", but nothing worked so far. Hope, somebody can help me figure this out.
This is what I scripted so far (I get an AppleEvent timed out):
set DefaultPath to POSIX file "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Test"
set DestFolder to "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Destination"
set theFile to (choose file with prompt "Select file:" default location (DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell
The problem is that the Finder doesn't support POSIX paths.
I recommend to use a relative path path to library folder from user domain and HFS paths (colon separated)
To satisfy the location parameter of choose file put the alias keyword in front of the (HFS) path
set cloudDocs to (path to library folder from user domain as text) & "Mobile Documents:com~apple~CloudDocs:"
set DefaultPath to cloudDocs & "FOLDER:Test:"
set DestFolder to cloudDocs & "FOLDER:Destination:"
set theFile to (choose file with prompt "Select file:" default location (alias DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell

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 not complying due to image

I'm using below code to load an image:
alias ((path to me) & "Contents:Resources:FCPXporter.icns" as string))
But I'm getting the error:
error "File alias Macintosh HD:Users:apple:Downloads:FCPXporter_Version_3.1.scptContents:Resources:FCPXporter.icns of «script» wasn’t found." number -43
How do I fix it?
The recommended syntax is
alias ((path to me as string) & "Contents:Resources:FCPXporter.icns")
It handles the path separators reliably.
But your script doesn't have a Resources folder because it has been saved as regular compiled script.
Two possible solutions:
Save the file as script bundle (.scptd), put the icon in the – now present – Resources folder and use the code above.
If the icon is supposed to be on the same level as the script use
set myself to path to me
tell application "System Events" to set parentFolder to (path of container of myself)
set theImage to (parentFolder & "FCPXporter.icns") as alias
In your code, your script file has the extension ".scpt" but your script file needs to be saved as either a script bundle (.scptd) or an application (.app) to have a "Resources" folder
set theImage to (path to me as string) & "Contents:Resources:FCPXporter.icns" as alias
-- Returns value.. alias "Macintosh HD:Users:apple:Downloads:FCPXporter_Version_3.1.scptd:Contents:Resources:FCPXporter.icns"
It looks like you are missing a path separator - try changing:
alias ((path to me) & "Contents:Resources:FCPXporter.icns" as string))
to
alias ((path to me) & ":Contents:Resources:FCPXporter.icns" as string))
If that still doesn't work then check that the file actually exists at the specified location - go to Terminal and:
ls "Macintosh HD/Users/apple/Downloads/FCPXporter_Version_3.1.scpt/Contents/Resources/FCPXporter.icns"

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