Custom "copy as Pathname" in Finder
Is there a way to somehow customize "copy as Pathname" in Finder (write-click, option, ...) so that the pathname that is copied is modified (e.g. to replace my username with somebody else's)?
Solution 1:
Any tips on how to approach this with Automator?
The example AppleScript code, shown below, was tested in Script Editor and as an Automator Service/Quick Action under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
- 1 Assumes any necessary and or appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
The following example AppleScript code can be used In Automator Service/Quick Action setting Workflow receives current [files or folders] in [Finder] and adding a Run AppleScript action replacing the default code with the example AppleScript code and setting the Options of the Run AppleScript action to: [√] Ignore this action's input
As currently coded you can set the diffShortUserName
variable to whatever, it's currently foobar
, or you can comment out the first line of code and uncomment the commented block of code, which will present a dialog box for you to enter a different short user name each time you use the Automator Service/Quick Action.
This will place on the clipboard the POSIX pathname of any selected files or folders in the frontmost window of Finder, from anywhere in your Home folder.
Example AppleScript code:
set diffShortUserName to "foobar"
(*
set diffShortUserName to ""
repeat while diffShortUserName is ""
set diffShortUserName to ¬
text returned of (display dialog ¬
"Different Short User Name:" default answer "")
end repeat
*)
tell application "Finder" to ¬
set selectedFiles to ¬
(selection as alias list)
set myShortUserName to short user name of (system info)
set selectedFilesCount to count selectedFiles
set thePathnamesList to {}
repeat with thisFile in selectedFiles
set thePathname to the POSIX path of thisFile
if thePathname contains "/Users/" & myShortUserName & "/" then
set {TID, AppleScript's text item delimiters} to ¬
{AppleScript's text item delimiters, myShortUserName}
set foo to text items of thePathname
set AppleScript's text item delimiters to diffShortUserName
set thePathname to foo as string
set AppleScript's text item delimiters to TID
if selectedFilesCount is greater than 1 then
copy thePathname & linefeed to end of thePathnamesList
else
copy thePathname to end of thePathnamesList
end if
end if
end repeat
set the clipboard to thePathnamesList as string
Notes:
If you are looking to replace your short user name that is elsewhere in the POSIX pathname of any selected files or folders in the frontmost window of Finder not in your Home folder, then additional coding will be necessary.
One can also assign a keyboard shortcut to the Automator Service/Quick Action in System Preferences > Keyboard > Shortcuts > Services, however, some prefer to use a third-party application that is capable of running AppleScript code and assigning a keyboard shortcut to it.
As an example, I use FastScripts for some tasks, as well as Hammerspoon for other tasks. (I am not associated with the developers of these products, just a satisfied user.)
This is also why I've coded it in the manner I did, removing the default code and setting the option in the action I did, as it makes the example AppleScript code available to work in the different methods in which it can be used.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.