I want to look into a folder to select every file whose name contains "abc".
Here is my AppleScript code:
set myFolder to (((path to library folder from user domain) as string) & "FOLDER") as alias
tell application "Finder"
set deleted123 to every file of folder myFolder whose name contains "abc"
repeat with oneFile in deleted123
if exists (deleted123) then set end of deleted123 to oneFile & return
--Do something
end repeat
if deleted123 ≠ {} then
--Do something else with the selected.
end if
end tell
The code works flawlessly on High Sierra, i.e., it finds out all the files whose names contain "abc", but it doesn't on Monterey.
What is the problem? How can this piece of code be improved?
Help highly appreciated.
There are two major mistakes:
myFolder is an alias specifier. You add the folder keyword which is a double reference, delete as alias in the first line
oneFile is a Finder file specifier, you cannot append the string return. Maybe the file specifier is silently coerced to string in Sierra. If you want to use the file references then the return statement makes no sense anyway.
Another bad practice is to modify the array deleted123 while being enumerated. Create an extra variable.
And the repeat loop makes no sense either because exists (deleted123) is always true and even if you check the current item in the loop oneFile it's always true.
Related
I am not a programmer by trade, but an art director on a rather large project where I do many repetitive tasks on a daily basis. One constant is moving files to team member's folders. I want to be able to select some files and have a menu appear for me to choose who will receive the files.
I made some progress but am stuck. The script cannot find the directory I intend the files to go. I fear it has something to do with POSIX paths - or me not defining it as a folder rather than a file? The error reads:
error "File /Volumes/Workbench/Test/CookieMonster wasn’t found." number -43 from "/Volumes/Workbench/Test/CookieMonster"
...even thou there is a folder where it says there isn't. I suspect my syntax is incorrect somehow.
set currentFolder to path to (folder of the front window) as alias
on error -- no open windows
set the currentFolder to path to desktop folder as alias
end try
set artistList to {"CookieMonster", "BigBird", "Bert", "Ernie", "Grouch"}
set assignTo to {choose from list artistList with title "Assign to..." with prompt "Please choose" default items "Bert"}
set assignedArtist to result
set artDeptDir to "/Volumes/Workbench/Test/"
set filePath to {artDeptDir & assignedArtist}
set destinationFolder to filePath as alias
set selected_items to selection
repeat with x in selected_items
move x to folder of filePath
end repeat
If anyone who has insight into my problem, I would love to hear from them. Thank you for taking the time to read this.
The main issue is that the Finder doesn't accept POSIX paths. You have to use HFS paths (colon separated and starting with a disk name).
This version fixes another issue and redundant code was removed
tell application "Finder"
try
set currentFolder to (target of the front window) as alias
on error -- no open windows
set currentFolder to path to desktop
end try
set artistList to {"CookieMonster", "BigBird", "Bert", "Ernie", "Grouch"}
-- no braces {} !
set assignTo to (choose from list artistList with title "Assign to..." with prompt "Please choose" default items "Bert")
if assignTo is false then return
set assignedArtist to item 1 of assignTo
-- no braces {} !
set destinationFolder to "Workbench:Test:" & assignedArtist
move selection to folder destinationFolder
end tell
This is my very first use of AppleScript. I'm trying to use it to move a 100+ folders (and their content files) from one location to another. I'm not just moving the files because I want to maintain the filing system. So I've got the folders listed in a .txt file in this format:
Macintosh HD:Users:Tom:Music:iTunes:Afrika Bambaataa
Macintosh HD:Users:Tom:Music:iTunes:Air
And then I run the following in AppleScript:
tell application "Finder"
set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
set My_Folder to (choose folder with prompt "Select your destination folder")
set List_files to paragraphs of (read TextFile)
move List_files to My_Folder
end tell
However the error I receive is the following:
error "Finder got an error: Handler can’t handle objects of this class." number -10010
Any help on what I'm doing wrong here?
Much appreciated!
tell application "Finder"
set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
set My_Folder to (choose folder with prompt "Select your destination folder")
set List_files to paragraphs of (read TextFile)
move List_files to My_Folder
end tell
Currently, List_files is just a list of text objects, i.e.
{"Macintosh HD:Users:Tom:Music:iTunes:Afrika Bambaataa",
"Macintosh HD:Users:Tom:Music:iTunes:Air", ...}
so what you're asking Finder to do is move some text to a folder, which doesn't make sense. You need to tell Finder that this text represents a path to a folder, by using the folder specifier. Since this can't be done en masse, you have to iterate through the list:
repeat with fp in List_files
move folder fp to My_Folder
end repeat
However, I wouldn't actually do it like this, as that will require 100+ separate move commands - one for each item in List_files. Instead, we'll edit the list items first by prepending the folder specifier to each item, and then move the list of folders altogether in a single command:
repeat with fp in List_files
set fp's contents to folder fp
end repeat
move List_files to My_Folder
Depending how big the files are and how long the transfer will take, the script might time out before the transfer is complete. I'm not honestly sure what impact this would have on an existing file transfer. I suspect that AppleScript will simply lose its connection with Finder, but that the transfer will continue anyway since the command has already been issued (another benefit of using a single move command instead of multiples). But, if you want to avoid finding out, we can extend the timeout duration to be safe:
with timeout of 600 seconds -- ten minutes
move List_files to alias "Macintosh HD:Users:CK:Example:"
end timeout
The final script looks something like this:
tell application "Finder"
set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
set My_Folder to (choose folder with prompt "Select your destination folder")
set List_files to paragraphs of (read TextFile as «class utf8»)
if the last item of the List_files = "" then set ¬
List_files to items 1 thru -2 of List_files
repeat with fp in List_files
set fp's contents to folder fp
end repeat
move List_files to My_Folder
end tell
The extra line that starts if the last item of... is just a safety precaution in case the last line of the text file is a blank line, which is often the case. This checks to see if List_files contains an empty string as its last item, and, if so, removes it; leaving it in would throw an error later in the script.
EDIT: Dealing with folders that don't exist
If the repeat loop throws an error due to an unrecognised folder name, then we can exclude that particular folder from the list. However, it's easier if we create a new list that contains only the folders that have been verified (i.e. the ones that didn't throw an error):
set verified_folders to {}
repeat with fp in List_files
try
set end of verified_folders to folder fp
end try
end repeat
move the verified_folders to My_folder
This also means we can delete the line beginning if the last item of..., as the check that this performs is now going to be caught be the try...end try error-catching block instead.
I'm trying to find the folder which has been last modified. (Actually, I'm only interested in that folder and not an ordered list.) I'm getting an -10010 error.
tell application "Finder"
try
set latestFolder to item 1 of (sort (get name of folders of folder ("/Users/c64/Desktop" as POSIX file)) by creation date) as alias
set folderName to latestFolder's name
end try
end tell
If you're looking for the name of the last modified folder on the Desktop, then this will do it:
tell application "Finder"
set latestModifiedFolderName to name of item 1 of (sort every folder by modification date)
end tell
By the way, the AppleScript Dictionary for Finder does not contain terms POSIX file or POSIX path and when using e.g. POSIX file inside of a tell application "Finder" block, Finder will throw a non-fatal error if it can be coerced into an alias, otherwise it can throw a fatal error. That said, if you are dealing with a POSIX path, it's probably best to pass it to Finder as an alias, and I'd recommend coercing the POSIX path to an alias before passing it to Finder, e.g.:
set thisFolderPath to POSIX file "/Path/To/Some/Folder" as alias
tell application "Finder"
set latestModifiedFolderName to name of item 1 of (sort every folder of thisFolderPath by modification date)
end tell
Note: The example AppleScript code above is just that, and does not include any error handling as may be appropriate/needed/wanted, the onus is upon the user to add any error handling for any example code presented and or code written by the oneself.
I am new to applescript and am tying to automate a project i am working on. I will not load the code because it is 4 miles long (there is probably a more efficient way) but the general sense is something along the lines of:
if directory ~/Library/Application' 'Support/kaiotemp exists then
do code 1
else
do code 2
end
I have tried my hardest but cant even come up with a base model that doesn't crash. Any ideas?
The application Finder knows how to determine if a file or folder exists. Try this...
set folderPath to (path to home folder as text) & "Library:Application Support:kaiotemp:"
set folderExists to false
tell application "Finder"
if folder folderPath exists then set folderExists to true
end tell
if folderExists then
--do code 1
else
--do code 2
end if
Notice my path versus your path. Applescript uses colon ":" delimited paths and the path starts with the name of your hard drive (I used the "path to" command to find the path to your home folder directly in this case). So you will need to take that into account if you have other paths in your code. You'll have to study how to convert posix paths to applescript paths.
I need to update the script shown below to find a new file type and naming system and have no idea what I'm doing. It use to pull a file named DQXXXXX1.eps and placed in the listed location. The new files are XXXXX_random.pdf. The random in the file name is several series of numbers that change for each file. The important numbers are the first 5, I would like the script to pull all files in that initial location and place into the other location.
The current script is:
set DQfolder to alias "Prepress:ArtFiles:00-Logos A to Z:1-DQ:"
tell application "Finder"
display dialog "enter number" default answer ""
set theNum to text returned of result as string
--try
move alias (DQfolder & "DQ" & theNum & ".eps" as string) to "Macintosh HD:__DQ Incoming:" with replacing
--on error {}
--move alias (DQfolder & "DQ" & theNum & ".tif") to "Macintosh HD:__DQ Incoming:" with replacing
--end try
end tell
Try your script like the following. You have a few things that need to be fixed...
Basically you are adding the strings wrong. You need to first coerce DQfolder to a string before you can add the other strings to it. The way you are doing it can't work because DQfolder is an alias so you can't add other strings to it until you coerce the alias to a string.
the folder path in your command has to be an alias-type file or a folder specification. As such you need to put the word "alias" or "folder" in front of it. A string path will not work. Applescript rarely works with string paths.
"display dialog" is not a Finder command and as such you don't need to put it in the Finder block of code.
"text returned" from your display dialog command is already "text" so you do not need to coerce it to a string. That's why it's called "text" returned... even if you entered a number it's class is still text.
Of course I can't check this code because I don't have files at those paths, but it should work. Good luck...
set DQfolder to alias "Prepress:ArtFiles:00-Logos A to Z:1-DQ:"
display dialog "enter number" default answer ""
set theNum to text returned of result
tell application "Finder"
move alias ((DQfolder as text) & "DQ" & theNum & ".eps") to alias "Macintosh HD:__DQ Incoming:" with replacing
end tell