Search code examples
pathapplescriptadobe-brackets

Path to document. Adobe Brackets


Is there any way I can get the path of the front document in Brackets?

What I have tried:

With AppleScript this works well with most of the code editors but not with Brackets:

tell window 1 to set thePath to value of attribute "AXDocument"

On the other side, I have noticed that in the Brackets (menu bar) > Window menu there is something like:

/path/to/document.html (some-text) — Brackets

I would like to get only the path, without: (some-text) — Brackets

(Be aware that (some-text) I mean it is a text that is different in each computer. The common case is that is inside the ().


Solution

  • Running the following:

    tell application "System Events" to ¬
        get has scripting terminology of application process "Brackets"
    

    Returns:

    false
    

    So, scripting Brackets with AppleScript will be limited; however, the following example AppleScript code worked for me to return the wanted portion of what shows for the document name on the Window menu in Brackets:

    tell application "System Events" to set menuItemName to ¬
        (name of menu item 7 of menu 1 of menu bar item ¬
            "Window" of menu bar 1 of application process "Brackets")
    
    set docPathName to do shell script ¬
        "grep -o '^.*\\.[[:alnum:]]*[^ ]'<<<" & ¬
        quoted form of menuItemName
    
    return docPathName
    

    Note that the grep command, if used in Terminal, the regex used for it would only have one backslash preceding the . to escape it to treat it as a literal .; however, when using the do shell script command, an additional backslash is necessary to escape it properly.


    In Brackets, with a document opened and selected under Working Files, the name that shows on the Window menu for the selected document is always menu item 7 and in the form of:

    /path/to/filename.ext (some text) — Brackets

    Using grep -o to return only the matched portion of the what's in the docPathName variable using the ^.*\.[[:alnum:]]*[^ ] regex, it will return everything up to but not including " (some text) — Brackets".

    In other words, it only returns the "/path/to/filename.ext" portion.

    Understanding the do shell script command:

    • grep - From its man page: file pattern searcher
      • -o - Prints only the matching part of the lines.
      • ^.*\.[[:alnum:]]*[^ ] - See Understanding the RegEx: below.
    • <<< - From the bash man page: Here Strings
      A variant of here documents, the format is: <<<word
      The word is expanded and supplied to the command on its standard input.

    Understanding the RegEx:

    • ^.*\.[[:alnum:]]*[^ ]
      • ^ - Asserts position at start of the string.
      • . - Matches any character (except for line terminators).
      • * - Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy).
      • \. - Matches the character . literally (case sensitive).
      • [[:alnum:]] - Match a single character present in the list, matches an alphanumeric character [a-zA-Z0-9].
      • * - Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy).
      • [^ ] - Match a single character not present in the list, matches the character literally (case sensitive).



    Note: The example AppleScript code is just that and does not contain any 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.