Create apple script which auto names files with info from clipboard when a key combination is pressed

applescriptautomatorcopy/pasterenameterminal

I work with file sets all beginning with the same number, but with variations, e.g. 0545038773.jpg, 0545038773_right.jpg, 0545038773_left.jpg, 0545038773_TOP.jpg, etc

I'd like to assign the, _left, _right, _TOP, etc. to specific key combinations like Command or Option and 1 pressed at the same time, OPT3, OPT4, OPT5, etc.

Ideally, you'd copy the first part, 0545038773 from wherever, then when you select the file and press the key command, OPT+1 for example, the script highlights the name field, places the copied info from clipboard, 0545038773, then appends the _left, or _right, depending on which key combo is pressed.

Any help would be appreciated. I had something that did this for PC but I don't have access to the script anymore to try and start translating any of it.

Even the first part would help. Select file, press key command, naming field then highlights, pastes from clipboard. Thanks.

Best Answer

This is a problem of two halves. The second half is easy:

    tell application "Finder" to get the selection as alias list

    tell result
        if (count it) is not 1 then return
        set TheFile to its first item
    end tell

    tell application "Finder" to set the name of TheFile to ¬
        [the clipboard, "_left", ".jpg"] as text

The core line of code you want here is the penultimate line set name of TheFile to..., where TheFile is a variable that points to the currently selected item in Finder, and you simply need to change the appending text item as you see fit. The first few lines simply deal with getting the selection from Finder and making sure only one file is selected.

The first half of your problem is solvable but at varying degrees of investment versus reliability.

Do you use any automation software, such as Alfred, Better Touch Tool, Keyboard Maestro or FastScripts ? All of these let you create hotkeys that trigger a macro to do whatever you want (in this case, run the script above). This would be my recommended method of doing things, but they are all paid-for applications.

The other way is to create a service in MacOS (using Automator). Once you’ve done that, you can go into System Preferences > Keyboard > Shortcuts > Services, find it in the list, check the box, and assign a shortcut to it like the ones you mentioned.

From then on, whenever you press those shortcuts, the service runs and executes your AppleScript.

Hope this helps. If you require clarification, leave a comment and I’ll get back to you.