Copy filename of file selected in Finder to clipboard using Automator with Applescript

applescriptautomator

I'm trying to create an Automator Application that begins with selecting a Finder item.

Before continuing I would like to copy the filename only, not the pathname to the clipboard.

How can I do that?

Thanks very much in advance!

Best Answer

To place the name of the Finder item without its extension onto the clipboard, you will need to add a Run AppleScript action with the following example AppleScript code:

on run {input, parameters}
    
    set fileName to first item of input
    
    tell application "System Events" to ¬
        tell disk item (fileName as text) to ¬
            set {theName, theExtension} to ¬
                {name, name extension}
    
    if theExtension is not "" then
        set theName to ¬
            text 1 thru -((count theExtension) + 2) ¬
                of theName
        set the clipboard to theName
    else
        set the clipboard to theName
    end if
    
    return input
    
end run

The return input in the example AppleScript code will pass the Finder item(s) onto the next action in the workflow.


Note:

As coded it will only place the name of the first selected Finder item, without its extension, onto the clipboard, as per your comment.