Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode 5 - AppleScript - How to get document in current tab

I want to open a document in the current tab in an external application (MacVim for example). Based on a StackOverflow answer I created an Automator service with following AppleScript code:

    tell application "Xcode"
        set current_document to last source document
        set current_document_path to path of current_document
    end tell

    tell application "MacVim"
        activate
        open current_document_path
    end tell

The issue is, that it opens the file from the first tab and not from the current tab. How can I get the path of the current tab?

like image 981
devmake Avatar asked Feb 03 '26 18:02

devmake


1 Answers

Following workaround based on SO answer works for me.

As noted in the comment there: This works EXCEPT if you are using the Assistant editor - then you end up with whatever happens to be in the Standard Editor. – Lloyd Sargent but for me it's better than the first tab.

on run {input, parameters}

    set current_document_path to ""

    tell application "Xcode"
        set last_word_in_main_window to (word -1 of (get name of window 1))
        if (last_word_in_main_window is "Edited") then
            display notification "Please save the current document and try again"
            -- eventually we could automatically save the document when this becomes annoying
        else
            set current_document to document 1 whose name ends with last_word_in_main_window
            set current_document_path to path of current_document
        end if
    end tell

    tell application "MacVim"
        if (current_document_path is not "") then
            activate
            open current_document_path
        end if
    end tell

    return input
end run
like image 132
devmake Avatar answered Feb 05 '26 09:02

devmake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!