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)
Related
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
I'm having trouble accessing this file while trying to select it on the beginning characters basis...
set location to "/Users/myuser/Desktop/"
set bom to POSIX file (location & (first file of location whose name begins with "thisFile"))
tell application "Preview" to open bom
is it path/alias vs text type of a thing?
Only System Events and the Finder know what a file in the file system is.
The Finder has a property desktop which points always to the desktop of the current user.
tell application "Finder" to set bom to first file of desktop whose name begins with "thisFile"
tell application "Preview" to open (bom as alias)
Or with an arbitrary POSIX path
set location to POSIX file "/Users/myuser/Desktop" as text
tell application "Finder" to set bom to first file of folder location whose name begins with "thisFile"
tell application "Preview" to open (bom as alias)
The alias coercion is needed because Preview doesn't recognize Finder file specifier objects.
vadian's answer works well, but it's worth mentioning that:
you can get access to well-known folders even in the default context, outside the context of System Events and Finder; e.g.:
path to desktop
path to home folder
Use, e.g., POSIX path of (path to home folder) to get the POSIX path.
using context System Events is usually preferable to the Finder context, for reasons of both performance and predictability.
With an arbitrary target folder, using a POSIX path:
tell application "System Events"
set targetFolder to alias "/Users/jdoe/Desktop"
# equivalent of: set targetFolder to (path to desktop)
set targetFile to first file of targetFolder whose name starts with "thisFile"
end tell
tell application "Preview" to open targetFile
Alternatively, if you know your way around the shell, you could try:
set targetFilePosixPath to do shell script "fls=(~/Desktop/*.pdf); printf %s \"$fls\""
tell application "Preview" to open (POSIX file targetFilePosixPath as alias)
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
How should an Apple Script look like to automatically open a file named testfile downloaded to the Downloads folder and run with a program testprogram?
I feel it should be a pretty simple template, and I am having trouble finding much to accomplish it.
This example uses Microsoft Word to open testFile.rtf. Update the code with your info and attach the folder action to your target folder.
on adding folder items to theFolder after receiving theFiles
set appPath to quoted form of (POSIX path of (path to applications folder) & "Microsoft Office 2011/Microsoft Word.app")
repeat with aFile in theFiles
tell application "Finder" to aFile's name = "testfile.rtf"
if the result then
set filePath to quoted form of aFile's POSIX path
do shell script "open -a " & appPath & space & filePath
end if
end repeat
end adding folder items to
If you manually want to open the file, try this:
set appPath to quoted form of (POSIX path of (path to applications folder) & "Microsoft Office 2011/Microsoft Word.app")
set filePath to quoted form of (POSIX path of (path to documents folder) & "testFile.rtf")
try
do shell script "open -a " & appPath & space & filePath
end try
You could create a folder action like this with Automator:
Finder's open command also has a using specifier for specifying the application:
tell application "Finder"
open POSIX file "/usr/share/doc/bash/bash.html" using (path to application "Safari")
end tell
I've been trying to get System Events to duplicate files in AppleScript and I've been failing :) I eventually always get the error "error "Files can not be copied." number -1717". So I changed my tactics and tried using the Finder to make sure what i was trying to do was correct. Here is the code that works:
tell application "System Events"
set desktopFolder to (path to desktop folder) as string
set fullPath to desktopFolder & "Temp Export From DO"
set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
set source to path of DOEntry
log "Source file: " & source
set destination to fullPath as string
log "Destination folder: " & destination
tell application "Finder"
duplicate file source to folder destination with replacing
end tell
end repeat
end tell
If I remove that last tell, so that it uses System Events, I get the same error noted above. The dictionary for System Events standard suite has a "duplicate" command so I'm not sure what is going on here. Also, "Learning AppleScript, 3rd ed" from APress notes:
"One particularly annoying omission in System Events is that it can’t yet duplicate files and folders; if you need to do this, the Finder is your best bet."
The 3rd edition is from 2010. It would seem that even in Mountain Lion this is still true. Can anyone confirm this? The 1717 error number lists everywhere else as a handler error and i'm not using handlers.
Unfortunately, you cannot duplicate files using System Events - you have to use the Finder. Even in the answer provided by adayzdone, System Events is not actually handling the duplication.
This looks like it's working (because it's inside a System Events tell block)...
tell application "System Events"
duplicate myFile to myFolder
end tell
...but if you inspect the event log you'll see that the Finder is actually performing the duplication. Behind the scenes, you are passing two Finder objects to System Events. System Events doesn't know how to handle Finder objects, so execution is passed to the objects' owner, the Finder, which executes the command.
For file duplication in AppleScript, you are unfortunately limited to using the Finder or the command line via do shell script.
Try:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
log "Source file: " & DOEntry
log "Destination folder: " & desktopFolder
tell application "Finder" to duplicate file DOEntry to desktopFolder with replacing
end repeat
If you don't need to log the values you can simply:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
tell application "Finder" to duplicate theDOEntries to desktopFolder with replacing
Or:
set desktopFolder to quoted form of (POSIX path of (path to desktop folder as text) & "Temp Export From DO")
do shell script "find '/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries' -name \"*.doentry\" -type f -print0 | xargs -0 -I {} cp -a {} " & desktopFolder
Getting back to your question, the duplicate commands creates duplicates of Finder items. You can use System Events to duplicate Finder items like this:
tell application "Finder"
set myFile to file ((path to desktop as text) & "Test File.txt")
set myFolder to folder ((path to desktop as text) & "Test Folder")
end tell
tell application "System Events"
duplicate myFile to myFolder
end tell