Copy filename from the clipboard and rename selected item (using Path Finder)

Hi all,

I have a macro that allow me to copy a filename (without the extension) from a selected file in Path Finder app:

I am trying to use a bit of Applescript to my new, separate macro and paste that filename from a clipboard.

Any suggestions?

Peter

Paste it where? You can paste it just with the Paste action or Command-V? What is the AppleScript supposed to do?

Hi Peter,

What I’ve achieved so far is “copy the file name without the extension” (which I did in my macro presented on the screenshot).

Next step would be to create a macro which paste that name on a selected file. I could record all the keystrokes ‘Return’ -> ’ CMD + V’ -> ‘Return’, however they are not seem to be robust at all (sometimes a macro will change a selection for some reason to a file above or below of my initial selection!), that’s why I prefer to use Applescript: it’s faster and consistent.

If you want to rename the selected file in the Finder, don’t use UI actions, use the Finder Selection actions.

Hey Peter,

You mean a file different from the file you got the name from?

###Save name of selected item in Path Finder to Keyboard Maestro variable.

Since you need to use AppleScript for this step it is more efficient to do all of it within the script. This is easily done, since we can get both the name and name extension of the selected item from Path Finder.

---------------------------------------------------------------------
# Get Name of Selected Item in Path Finder without name extension.
---------------------------------------------------------------------
tell application "Path Finder"
  set pfSelection to the selection
  
  if length of pfSelection = 1 then
    set theItem to item 1 of pfSelection
    set itemName to name of theItem
    set fileExtension to name extension of theItem
    
    if fileExtension ≠ "" then
      set AppleScript's text item delimiters to "." & fileExtension
      set itemName to (text items 1 thru -2 of itemName)
      set AppleScript's text item delimiters to ""
      set itemName to itemName as Unicode text
    end if
    
  else
    beep
  end if
end tell

tell application "Keyboard Maestro Engine"
  set kmVariableName to "pfSavedName"
  try
    set value of variable kmVariableName to itemName
  on error
    make new variable with properties {name:kmVariableName, value:itemName}
  end try
end tell
---------------------------------------------------------------------

###Rename the selected item in Path Finder.

Because scripting in Path Finder is broken in places this is harder than it should be

It’s necessary to use the Finder to do the rename, but it works alright and updates quickly in PF.

---------------------------------------------------------------------
# Rename Selected Item in Path Finder.
------------------------------------------------------------
tell application "Keyboard Maestro Engine"
  set kmVariableName to "pfSavedName"
  if length of (get variables whose name is kmVariableName) = 1 then
    set pfSavedName to value of variable kmVariableName
  else
    error "Variable '" & kmVariableName & "' does not exist!"
  end if
end tell

tell application "Path Finder"
  set pfSelection to the selection
  
  if length of pfSelection = 1 then
    set theItem to item 1 of pfSelection
    set theItem to POSIX path of theItem
    
  else
    beep
  end if
end tell

set theItem to alias POSIX file theItem

tell application "Finder"
  set name of theItem to pfSavedName
  update parent of theItem
end tell

---------------------------------------------------------------------

* Note: I’ve file a bug report with Cocoatech about renaming items being broken, but it will be more effective if others do the same.

-Chris

Hi Chris,

Wow that some amount of code! I can’t make this work for me unfortunately (I mean nothing is happening), my guess is I am doing something wrong here :confused:

I’ll try to explain it again:

  1. I have a workable macro (CTRL + C) that copies the filename (once I manually select the file) without the extension, as a plain text to the System Clipboard. This works perfect for me.

  2. Now, I am trying to create another Macro (let’s string it to CTRL + V) which paste that plain text onto a selected file (located in a different folder) instantly. What I mean by that is ‘if I would do it manually which involves more actions and takes more time: select the file - > Return Key -> Paste (Plain text from the clipboard) -> Return Key’.

Peter

Assuming you will select the file manually to rename, then the process is fairly easily done as shown in the (Working with the Finder Selection topic.

I could be as simple as:

Rename File %Variable%Path% to %CurrentClipboard%

If you want to keep the extension, then you will need to do a bit more work, using the Get File Attribute action to get the current extension and include it in the rename.

Hey @peterstnsz,

You understand that these two AppleScript are separate actions?

You understand that AppleScripts are run via Execute an AppleScript actions? (Use the text-script option.)

One script copies the name of the selected item in Path Finder without the extension (if there is an extension) and saves it to a KM variable.

The other does not paste – rather it uses the Finder to rename the item selected in Path Finder.

(I heard from Path Finder support that setting the name of an item in PF with AppleScript is indeed broken.)

I just tested, and the two scripts work perfectly for me.

If you can’t get this working then post your macros, so I can see what you’re doing.

-Chris

Just curious...(I realize, I'm jumping in 4 years after the fact and so, only if this still is of interest), Peter 'S' : Where are the two files, the first one with a name you would like to copy and the second, onto which you would like to paste the name of the first.

For example, are both files sitting in separate folders on your desktop, or are they in your home folder or (?). This is important data that will guide you to an accurate resolution.