XCode 5 - AppleScript - 如何在当前选项卡中获取文档

时间:2014-03-19 08:02:15

标签: xcode applescript xcode5

我想在外部应用程序的当前选项卡中打开文档(例如MacVim)。基于StackOverflow answer我创建了一个Automator服务,其中包含以下AppleScript代码:

    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

问题是,它从第一个选项卡打开文件,而不是从当前选项卡打开。如何获取当前选项卡的路径?

2 个答案:

答案 0 :(得分:3)

基于SO answer的解决方法对我有用。

如评论中所述:如果您使用的是助理编辑器,则此工作除外 - 然后您最终会遇到标准编辑器中发生的任何情况。 - Lloyd Sargent 但对我而言,它比第一个标签好。

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

答案 1 :(得分:1)

尝试以下

tell application "Xcode"
    set docs to source documents
    if length of docs is less than 1 then
        display dialog "for ....... I need at least 1 document"
        return
    end if
    path of last item of source documents   
end tell