AppleScript: match and delete files in folder - macos

My photo camera allows to save pictures in RAW and JPG in parallel. I find this convenient because on my Mac I can quickly browse the JPGs and delete the "bad" ones. Besides, I keep the RAW files of the "good" JPGs in case I need to do some deep editing.
I would like to write an AppleScript which deletes all the "bad" RAWs (RAW files which don't have a corresponding JPG anymore). All files are in the same directory.
This is my outline (far away from correct syntax!):
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my clearFiles(source_folder)
end tell
on clearFiles(source_folder)
set theItems to ""
tell application "System Events"
set theItems to get the name of every disk item of source_folder
end tell
repeat with theFile in theItems
if the extension of theFile is "raw" and exists name of theFile & ".jpg" then
tell finder
delete (name of theFile & ".raw") in source_folder
and tell
end if
end tell
end clearFiles

try this
set source_folder to choose folder with prompt "Please select directory."
tell application "Finder"
set rawFiles to every file of source_folder whose name extension is "raw"
repeat with aFile in rawFiles
set baseName to text 1 thru -5 of (get name of aFile)
set jpgFile to baseName & ".jpg"
if not (exists file jpgFile of source_folder) then delete aFile
end repeat
end tell

Related

Inserting a file into a folder

I can copy the file using AppleScript.
But how can I use it to paste the copied file to another folder?
I couldn't find the right operator or similar question.
tell application "Finder" to set the clipboard to (POSIX file "/Users/bgbg/Library/Messages/chat.db")
It definitely seems odd to me to use the clipboard as the variable for copying and moving files or folders and to use the keystroke command to paste a file into a folder. However, if you are hell-bent on doing things this way, I do believe this following AppleScript code will accomplish what you’re looking to achieve
-- Set The Clipboard To A File (This Only Works With A Single File¬
-- Or Folder And Not With A List Of Files Or Folders)
-- set the clipboard to POSIX path of (choose file with prompt ¬
-- "Set The Clipboard To A Single File Or Folder" invisibles false ¬
-- without multiple selections allowed) as POSIX file
set the clipboard to POSIX path of (((path to library folder from user domain as text) ¬
& "Messages:chat.db") as alias) as POSIX file
set destinationFolder to (choose folder with prompt ¬
"Choose Your Destinations Folder" with invisibles)
tell application "Finder"
activate
repeat until frontmost
delay 0.1
end repeat
reveal destinationFolder
end tell
delay 1
tell application "System Events" to keystroke "v" using {command down}

Check if file names are the same using Applescript

How would I go about checking if a file (an alias called theFile) has the same name as any other files in another folder?
I would like to have an Applescript look through all of the files in the folder and has a if and else as a result if it does match one of the names or doesn't.
Choose a folder
Get the name of theFile
Get a list of all file names in the folder
Check if the list contains the name of theFile
set theFile to alias ...
set theFolder to choose folder
tell application "System Events"
set theFileName to name of theFile
set otherFileNames to name of files of theFolder
end tell
set fileNameExists to otherFileNames contains theFileName

Import contents of specific folder into iTunes using Applescript

I have created an Applescript that will, with one click, add the contents of a specific folder on my hard drive to iTunes (I really want to avoid using the iTunes organisational system).
Applescript
tell application "iTunes"
set theFolder to ("Macintosh HD:Temp to be Listened to:Temp on iPod:")
set with_subfolders to true --list subfolders or not (recursion)
set parent_folder to theFolder
end tell
tell application "System Events"
set item_list to parent_folder
end tell
tell application "iTunes"
add contents of parent_folder to user playlist "Temp on iPod"
end tell
However, it only imports into iTunes the files from the top level/parent folder.
I want to include files from the folders in the parent folder.
Is there a way I can get it to be recursive?
You do not need to get all files from a folder, since iTunes does this automatically and recursively from a folder.
Just add the parent folder, like this:
set parent_folder to "Macintosh HD:Temp to be Listened to:Temp on iPod:" as alias
tell application "iTunes"
add parent_folder to user playlist "Temp on iPod"
end tell
Solution #1: Using entire contents of the Finder - not very fast
set theFolder to "Macintosh HD:Temp to be Listened to:Temp on iPod:"
tell application "Finder" to set filesToAdd to files of entire contents of folder theFolder as alias list
tell application "iTunes" to add filesToAdd to user playlist "Temp on iPod"
Solution #2 : Using Spotlight - very fast, but the volume must be indexed.
set theFolder to "/Temp to be Listened to/Temp on iPod"
set fileList to paragraphs of (do shell script "mdfind -onlyin " & quoted form of theFolder & space & quoted form of "kMDItemContentTypeTree == '*public.audio*'")
set filesToAdd to {}
repeat with aFile in fileList
set end of filesToAdd to POSIX file aFile as alias
end repeat
tell application "iTunes" to add filesToAdd to user playlist "Temp on iPod"

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 Focus / Rename File (and clicking anywhere)

on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
return input
end run
Is what I have so far, and it works, but is there a way to then focus on the file then trigger a rename? I dont want the rename to be automatic, just trigger the event (like pressing "return" while you have a file selected). And I dont want to use any sort of modal...
Quick Side question: is there a way to set this so that i dont have to select a folder or file directly, but can do it by, lets say, clicking in a white space in a folder as long as it's in Finder? Right now I have my "Service receives selected" to "files or folders" in Finder.app.
== UPDATED CODE ==
on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
tell application "Finder"
activate
set target of Finder window 1 to POSIX file "/Users/oscargodson/Documents/designs/untitled"
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
return input
end run
If i hardcode the path it works! But how do I get it as a var that works?
Here's one way. I think a modal window where you ask for the name would be better but you can try this. Notice you do not use "POSIX path" in this code. Applescript doesn't use POSIX paths. Also {input}, as indicated by the brackets around it, is a list of items. Therefore you act on the items of the list, and in this case we act on the first item.
set filepath to item 1 of input
tell application "Finder"
activate
reveal filepath
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
EDIT: With your updated code, here's a working script...
on run {input}
if (class of input) is not list then set input to {input}
set theFolder to (item 1 of input) as text
try
alias theFolder
tell application "Finder"
if (class of item theFolder) is not folder then error "input is not a folder."
activate
set theFile to make new file at folder theFolder with properties {name:"untitled"}
reveal theFile
end tell
delay 0.2
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
on error theError number errorNumber
tell me
activate
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
return
end tell
end try
return input
end run
tell application "Finder"
activate
reopen -- in case there are no open windows
set target of Finder window 1 to POSIX file "/Applications/Safari.app"
end tell
reveal and select always open a new window, set target and set selection don't.
I don't know why, but when set selection it used in column view, you can only select items in the entire contents of the target of the front window. The same thing doesn't happen in other views, so it seems like a bug.
Fix for the code in the edited question:
on go(input)
set p to POSIX path of (input as text)
set p2 to p & "untitled"
do shell script "touch " & p2
tell application "Finder"
reopen
activate
set target of Finder window 1 to POSIX file p2
end tell
delay 0.3 -- time to release modifier keys
tell application "System Events" to keystroke return
end go
tell application "Finder"
set fold to folder (path to documents folder)
end tell
go(fold)
(That on go and the last lines are just for testing.)
I've created an AppleScript based on the #regulus6633's one, but with some improvements.
Note: This answer was originally posted as an AskDifferent answer. I'm copy/pasting here for convenience.
The idea is to create an Automator workflow and assigning a shortcut to it using the following steps:
Open Automator and create a Service;
Set the input to no input, and the application to Finder.app;
Drag and Drop the Run AppleScript workflow element onto the grey space;
Put the contents of this AppleScript in the textbox;
Save the workflow with a reasonable name (like New File);
Go to Settings -> Keyboard -> Shortcuts -> Services and assign a shortcut to it.
Now, let's show the AppleScript:
set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false
-- get folder path and if we are in desktop (no folder opened)
try
tell application "Finder"
set this_folder to (folder of the front Finder window) as alias
end tell
on error
-- no open folder windows
set this_folder to path to desktop folder as alias
set is_desktop to true
end try
-- get the new file name (do not override an already existing file)
tell application "System Events"
set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
if new_file is in file_list then
set new_file to file_name & " " & x & file_ext
set x to x + 1
else
exit repeat
end if
end repeat
-- create and select the new file
tell application "Finder"
activate
set the_file to make new file at folder this_folder with properties {name:new_file}
if is_desktop is false then
reveal the_file
else
select window of desktop
set selection to the_file
delay 0.1
end if
end tell
-- press enter (rename)
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
For convenience, I'm putting this AppleScript in this GitHub Gist.

Resources