【问题标题】:How to display Terminal results in textView field on Cocoa Applescript application如何在 Cocoa Applescript 应用程序的 textView 字段中显示终端结果
【发布时间】:2015-11-01 03:29:05
【问题描述】:

我正在使用 Xcode 7 开发一个 Cocoa Applescript 应用程序。该应用程序将运行简单的终端命令,例如 $ cd、$ ls、$ ./name 等。

我的问题是如何在 GUI 窗口的 textView 字段中显示终端结果(例如,当我键入“ls”时,它会显示我当前目录的内容列表)。

screenshot of my simple application

script AppDelegate

    property parent : class "NSObject"

    -- IBOutlets
    property theWindow : missing value

on changedirectoryClicked_(sender)
    tell application "Terminal"
        activate
        do script "cd"
        end tell
    end changedirectoryClicked_

on ifconfigClicked_(sender)
    tell application "Terminal"
        activate
        do script "ifconfig"
    end tell
end ifconfigClicked_

on exitClicked_(sender)
    tell application "Terminal"
        activate
        do script "exit"
    end tell
end exitClicked_

on lsClicked_(sender)
    tell application "Terminal"
        activate
        do script "cd"
    end tell
end lsClicked_

    on applicationWillFinishLaunching_(aNotification)
            -- Insert code here to initialize your application before any files are opened 
    end applicationWillFinishLaunching_

    on applicationShouldTerminate_(sender)
            -- Insert code here to do any housekeeping before your application quits 
            return current application's NSTerminateNow
    end applicationShouldTerminate_

结束脚本

【问题讨论】:

    标签: macos cocoa terminal applescript


    【解决方案1】:

    我有几个问题想了解您不知道如何完成此流程的哪一部分。您是否有意打开终端窗口? 通常,要从 shell 命令获取返回结果以便将其提供给 textView,您需要使用“do shell script”applescript 命令。

    on lsClicked:sender
        set theResult to do shell script "ls"
        setTextView(theResult)
    end lsClicked:
    

    请注意,您必须在 do shell script 命令中使用 ls 命令进行管道和 cd 更改。

    【讨论】:

    • 我想单击一个按钮以运行终端命令。然后,我希望所述终端命令的结果显示在我的 GUI 中的 texView 字段中。我实际上希望终端窗口保持隐藏状态,我不希望桌面被窗口淹没。
    • 好吧,这个例子会满足你的要求。
    【解决方案2】:

    好的,首先你需要在窗口下为文本字段创建一个属性并连接它:

    property myTextField : missing value -- Connect to Text Field
    

    现在要将终端结果返回到您所做的文本字段中:

    on chooseDirectory_(sender)
        set choosenDirectory to (choose folder with prompt "choose a directory to list")
    end chooseDirectory_
    

    我们还需要添加一个目录转换来将 AppleScript 目录更改为终端, 在 "set choosenDirectory etc.." 下,添加以下内容:

    set choosenDirectory2 to POSIX path of (choosenDirectory)
    set amountOfCharactersOfChoosenDirectory to count the characters of choosenDirectory2
    set choosenDirectory3 to ""
    set currentCharacterOfChoosenDirectory to 0
    repeat amountOfCharactersOfChoosenDirectory times
        set currentCharacterOfChoosenDirectory to currentCharacterOfChoosenDirectory + 1
        set currentLetterOfChoosenDirectory to character currentCharacterOfChoosenDirectory of choosenDirectory2
        if currentLetterOfChoosenDirectory is " " then
            set choosenDirectory3 to choosenDirectory3 & "' '"
        else
            set choosenDirectory3 to choosenDirectory3 & currentLetterOfChoosenDirectory
        end if
    end repeat
    

    好的,现在我们可以运行终端命令了:

    set theOutput to (do shell script "ls " & choosenDirectory3)
    

    终于可以在文本框中写了:

    tell myTextField to setStringValue_(theOutput)
    

    对不起,这有点高级,但这可能会有所帮助

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      相关资源
      最近更新 更多