Copy file name to clipboard upon saving file in macOS

18 Sep 2022

Use case: inserting filename of an image just saved in a note, avoiding selections, copy and paste.

Hazel does not copy to clipboard.

DefaultFolderX might have a workaround in v6, though not available yet on Setapp. And the workaround will also require an AppleScript.

Exploring Automator for this.

Useful ressource:

Working on a single test folder 😁

Setup:

automator-setup

on run {input, parameters}

    tell application "Finder"
        set filename to name of file input

    end tell
end run

with this, every file added to the given folder has its name (+ extension) copied to clipboard, ready to get pasted anywhere.

to copy full file path instead, just remove the AppleScript and keep only the "Copy to Clipboard" function. It copies the entire file, but reverts to file path when copied in a text editor.

Next: test on applying it to all subfolders of a given root folder.

Seems not possible with Automator alone, but perhaps combined with Hazel.

Need to make it work with an AppleScript alone, then trigger the script with Hazel.

set the clipboard to filename is command to copy to clipboard from within AppleScript :)

Works:

on run {input, parameters}

    tell application "Finder"
        set filename to name of file input
    end tell

    set the clipboard to name
end run

Not working:

hazel-applescript-1.jpg

TODO: need to figure out how to trigger it from Hazel - configure input/parameters.

links

social