Last active
March 14, 2021 17:12
-
-
Save mixio/7e3a909d391b30853c3cc39efb68d51a to your computer and use it in GitHub Desktop.
AppleScript for BBEdit that tries to use the selected text as a path or paths to files to open with the help of the terminal. This script should be compiled as an .scpt script before use.
This file contains 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
# Author: Jean Jourdain | |
# © 2021, mixio.com | |
# License: MIT | |
-- | |
use AppleScript version "2.7" | |
use scripting additions | |
use framework "Foundation" | |
-- | |
property NSString : class "NSString" | |
property NSCharacterSet : class "NSCharacterSet" | |
-- | |
(* | |
A BBEdit selection can be: | |
1. an insertion point when nothing is selected | |
- class of selection: insertion point | |
- length of selection: 0 | |
- get selection: insertion point before character x of text document 1 | |
- (properties of selection) as record: { | |
contents:"", | |
length:0, | |
characterOffset:254, | |
startLine:8, | |
endLine:8, | |
startColumn:17, | |
endColumn:17, | |
startDisplayLine:8, | |
endDisplayLine:8, | |
container:project window 1 of application "BBEdit"} | |
2. a characters specifier when user click-dragged a selection. | |
- class of selection: character | |
- length of selection: n > 0 | |
- get selection: characters x thru y of text document 1 | |
- (properties of selection) as record: { | |
contents:"blabla", | |
length:6, | |
characterOffset:2, | |
startLine:2, | |
endLine:2, | |
startColumn:1, | |
endColumn:7, | |
startDisplayLine:2, | |
endDisplayLine:2, | |
container:project window 1 of application "BBEdit"} | |
3. a list of characters specifiers when user option-click-dragged a selection. | |
- class of selection: text | |
- length of selection: error!!! | |
- get selection: {characters x1 thru y1 of text document 1, | |
characters x2 thru y2 of text document 1, ...} | |
- (properties of selection) as record: { | |
contents:"foo\nbar\n", | |
length:8, | |
characterOffset:4, | |
startLine:1, | |
endLine:2, | |
startColumn:5, | |
endColumn:8, | |
startDisplayLine:1, | |
endDisplayLine:2, | |
container:project window 1} | |
*) | |
on getNormalizedSelection() | |
tell application "BBEdit" | |
try | |
tell first document of first window | |
set vSelection to (properties of selection of its window) as record | |
set vContents to (contents of vSelection) as string | |
set vLength to (length of vSelection) as integer | |
set vCharacterOffset to (characterOffset of vSelection) as integer | |
set vStartLine to (startLine of vSelection) as integer | |
set vEndLine to (endLine of vSelection) as integer | |
set vStartColumn to (startColumn of vSelection) as integer | |
set vEndColumn to (endColumn of vSelection) as integer | |
set vStartDisplayLine to (startDisplayLine of vSelection) as integer | |
set vEndDisplayLine to (endDisplayLine of vSelection) as integer | |
set vContainer to container of vSelection | |
set vSelectionSpecifier to selection of its window | |
set vSelectionClass to class of (selection of its window) | |
if vSelectionClass is insertion point then | |
set vRunsCount to 0 | |
else if vSelectionClass is character then | |
set vRunsCount to 1 | |
else if vSelectionClass is text then | |
set vRunsCount to length of (vSelectionSpecifier as list) | |
else | |
error "Unsupported class of selection: '" & vSelectionClass & "'." | |
end if | |
return {contents:vContents, length:vLength, characterOffset:vCharacterOffset, startLine:vStartLine, endLine:vEndLine, startColumn:vStartColumn, endColumn:vEndColumn, startDisplayLine:vStartDisplayLine, endDisplayLine:vEndDisplayLine, container:vContainer, specifier:vSelectionSpecifier, class:vSelectionClass, runsCount:vRunsCount} | |
end tell | |
on error aMessage | |
display dialog aMessage | |
end try | |
end tell | |
end getNormalizedSelection | |
-- | |
on runInTerminal(aCommand) | |
tell application "Terminal" | |
tell selected tab of first window | |
set vTab to do script (aCommand) in it | |
end tell | |
end tell | |
end runInTerminal | |
-- | |
on trim(aString) | |
set vString to aString as string | |
set vNSString to NSString's stringWithString:vString | |
set vCharacterSet to NSCharacterSet's whitespaceCharacterSet | |
set vNSString to vNSString's stringByTrimmingCharactersInSet:vCharacterSet | |
return vNSString as string | |
end trim | |
-- | |
on escapePath(aString) | |
set vString to aString as string | |
set vNSString to NSString's stringWithString:vString | |
set vNSString to vNSString's stringByReplacingOccurrencesOfString:" " withString:"\\ " | |
return vNSString as string | |
end escapePath | |
-- | |
try | |
tell application "BBEdit" | |
set vSelection to my getNormalizedSelection() | |
set vRunsCount to runsCount of vSelection | |
set vCharacterOffset to characterOffset of vSelection | |
set vStartLine to startLine of vSelection | |
set vStartColumn to startColumn of vSelection | |
tell first document of first window | |
if vRunsCount = 0 then -- Nothing selected. | |
-- Use any non whitespace text surrounding or preceding the cursor. | |
set vLine to contents of its line vStartLine | |
set vLineLength to (length of vLine) as integer | |
tell AppleScript -- Use AppleScript's text vocabulary to process string. | |
set vEndColumn to vStartColumn - 1 | |
repeat while ((character vEndColumn of vLine) is not in {space, tab}) and (vStartColumn ≤ vLineLength) | |
set vEndColumn to vEndColumn + 1 | |
end repeat | |
set vLineHead to my trim(texts 1 thru vEndColumn of vLine) | |
set vDelimiters to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to {space, tab} | |
set vItems to text items of (vLineHead as string) | |
set AppleScript's text item delimiters to vDelimiters | |
if vItems = {} then | |
error "Please select a valid path." | |
end if | |
set vPath to last item of vItems | |
set vPaths to {vPath} | |
end tell | |
else -- Open each line of the selection separately. | |
tell AppleScript -- Use AppleScript's text vocabulary to process string. | |
set vPaths to paragraphs of contents of vSelection | |
end tell | |
end if | |
end tell | |
end tell | |
repeat with vPath in vPaths | |
set vEscapedPath to my escapePath(my trim(vPath)) | |
if vEscapedPath is not "" then | |
set vCommand to "open " & vEscapedPath | |
my runInTerminal(vCommand) | |
end if | |
end repeat | |
on error aMessage | |
display dialog aMessage | |
end try | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment