Last active
February 16, 2020 18:23
-
-
Save tru2dagame/600e29e082eaae280d91f746ed4918b0 to your computer and use it in GitHub Desktop.
Use apple script to open the selected file in finder with emacscilent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- get the file path from the finder | |
tell application "Finder" to set theItems to selection | |
set thePaths to "" | |
repeat with i in theItems | |
set thisItem to POSIX path of (i as alias) | |
set thePaths to thePaths & thisItem & return | |
end repeat | |
set filePath to thePaths | |
-- remove \n of the file path | |
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232} | |
set newText to text items of filePath | |
set AppleScript's text item delimiters to {" "} | |
set theText to newText as text | |
-- remove the last whitespace | |
set theText to trim(theText) | |
-- add escape: convert ' and & to \' and \& | |
set theText to searchAndReplace(theText, "'", "\\'") | |
set theText to searchAndReplace(theText, "&", "\\&") | |
-- add escape to whitespace | |
set theText to searchAndReplace(theText, " ", "\\ ") | |
-- use shell script to let the emacsclient work | |
do shell script "emacsclient -c -a emacs " & theText & " " | |
on trim(theString) | |
return (do shell script "echo \"" & theString & "\" | xargs") | |
end trim | |
on searchAndReplace(targetText, searchText, replaceText) | |
set newText to "" | |
set {tid, my text item delimiters} to {my text item delimiters, searchText} | |
try | |
set textList to every text item of targetText | |
set my text item delimiters to replaceText | |
set newText to textList as text | |
set my text item delimiters to tid | |
on error | |
set my text item delimiters to tid | |
set newText to targetText | |
end try | |
return newText | |
end searchAndReplace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just replace thedo shell script
part to point to theemacsclient
you have on your Mac.