Search code examples
fileautomationapplescriptrename

How can I tell my AppleScript to ignore renamed files?


I put together a simple AppleScript that automatically opens new files, that are added to a specific folder. This works great! Problem is, if I rename the new file, then automator runs and re-opens the document. The naming convention is limited by the scanner, but is:

  • 2022-12-21-191532
  1. Year-Month-Date "-"
  2. Random number (timestamp maybe?)
  3. ".pdf"

Is there a way to make my script only run on new files and ignore files that are renamed?

The settings are "AppleScript" & "on adding"

on adding folder items to this_folder after receiving added_items
    delay 30
    
    try
        tell application "Finder"
            --get the name of the folder
            set the folder_name to the name of this_folder
            --go to the desktop 
            activate
            open this_folder
            open added_items
            
            reveal the added_items
        end tell
    end try
end adding folder items to

I tried googling this, but all I keep finding are examples on how to automate renaming files.


Solution

  • Here is an AppleScript solution which you may find useful.

    This following AppleScript code will display a dialog allowing you to enter a new file name to rename your currently selected file, in Finder, to the new name. Before it renames the file, it will temporally disable all Folder Actions, then renames the file, then re-enables your Folder Actions. This way, simply renaming your file wont trigger your Folder Actions to run.

    tell application "Finder"
        if (count of (get selection)) ≠ 1 then
            tell current application
                activate
                display dialog "Please Make Sure Only One File Is Selected" buttons ¬
                    {"OK"} default button "OK" giving up after 4
            end tell
            return
        else
            set nameOfFile to name of item 1 of (get selection)
            tell current application
                activate
                set newName to text returned of ¬
                    (display dialog "Rename file " & nameOfFile & ¬
                        " to..." default answer "New File Name" buttons ¬
                        {"Cancel", "OK"} default button 2 cancel button 1 with icon 1)
            end tell
            set renameThisFile to item 1 of (selection as alias list)
        end if
    end tell
    
    tell application "System Events"
        if folder actions enabled then set folder actions enabled to false
        delay 0.1
        set name of renameThisFile to newName
        delay 0.1
        if not folder actions enabled then set folder actions enabled to true
    end tell
    

    What you could do is take the code from above, then in Automator create a new Quick Action, add a Run AppleScript command to the workflow and insert the AppleScript code.

    After you name and save your new Automator Quick Action, you can then shoot over to System Preferences/Settings and assign it a keyboard shortcut.

    Now anytime you are in Finder, you can rename your currently selected file with your new keyboard shortcut and you will not trigger your Folder Actions.

    I have a Folder Action that automatically adds the tag "Aquarium" to any item added to this one specific folder. This following animation demonstrates me first renaming "file 1.txt" to "file.txt" in Finder (the normal way). As you can see, simply remaining that file did trigger the folder action which automatically added the tag to the renamed file. Then, I used my new keyboard shortcut to rename the selected file "file 2.txt" to "xxx" in Finder, without triggering the Folder Action to add the tag to the file after renaming it.