Find & replace part of a file path

Hi there

I am wanting to creating an Apple Script that will grab the file path and then change the beginning of the path to be ~/ and then copy to the cipboard.


/Users/someUser/Dropbox/Some file.txt

and change it to

~/Dropbox/Some file.txt

The script I have so far looks like the below

tell application "Finder"
	set fp to the selection as text
	set fp2 to POSIX path of fp
end tell

do shell script "grep ^(.+)\Dropbox" 

Set the cipboard to fp3

The first applescript part works as expected getting the file path but I am unsure how to implement the grep shell script to make the change and copy that to the clipboard.

Any help appreciated.

Here is the little bit of a different approach but it should give you the results you’re looking for.

tell application "Finder" to set thisFilePath to POSIX path of (item 1 of (get selection) as alias)

set shortName to short user name of (system info)

set cleanedText to editPath(thisFilePath, ("/Users/" & shortName), "~")
set the clipboard to cleanedText

on editPath(someText, replaceTheseListItems, replacementListItems)
	set originalText to someText
	set text item delimiters to replaceTheseListItems
	set tempText to text items of originalText
	set text item delimiters to replacementListItems
	set cleanedText to tempText as text
end editPath

Another suggestion.

tell application "Finder" to set oldPath to selection as text
set oldPath to POSIX path of file oldPath
set homeFolder to POSIX path of (path to home folder as text)

set {TID, text item delimiters} to {text item delimiters, homeFolder}
set newPath to "~/" & text item 2 of oldPath
set text item delimiters to TID

set the clipboard to newPath

Here’s another solution that is as fast as @Peavine’s solution, but has support for handling selection error and then safely pasting the contents of the clipboard to the command line.

tell application "Finder" to set sel to (selection as alias list)

	set posixPath to POSIX path of item 1 of sel
	set home to POSIX path of (path to home folder)
	tell posixPath to set tildeQuotedPosixPath to "~" & quoted form of (text (count home) thru -1)
	set the clipboard to tildeQuotedPosixPath
on error
	display dialog "Finder selection is empty!"
end try

Another way is the Foundation way with stringByAbbreviatingWithTildeInPath

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

tell application "Finder" to set theSelection to selection
if theSelection is {} then return

set posixPath to POSIX path of (item 1 of theSelection as text)
set abbreviatedPath to (current application's NSString's stringWithString:posixPath)'s stringByAbbreviatingWithTildeInPath
set the clipboard to abbreviatedPath as text

I tested four suggestions with Script Geek. Timing was:

1st, 4th solutions - 5 msec
2nd, 3rd - 3 msec

The 4th solution requires loading framework, so it is very slow for first running. But, the 4th solution has one clear advantage over the 1st and 2nd: AsObjC can work further with an unquoted paths.

Another option…

tell application "Finder" to set thisFilePath to POSIX path of (selection as text)

set homeFolder to POSIX path of (path to home folder as text)

do shell script "echo " & thisFilePath & " | sed -E 's|" & homeFolder & "|~/|g' | pbcopy"

Thanks for all the great AppleScript answers and to KniazidisR for the speed testing analysis.

They all work and have the required output I was looking for.
Not sure which one I will go with but it will be interesting working out how/why each one works.

Thanks again for the help and insight :slight_smile: