【问题标题】:Applescript only works onceApplescript 只能运行一次
【发布时间】:2015-07-21 20:01:57
【问题描述】:

我仍在学习 AppleScript 的基础知识。 当我尝试运行一个简单的“Hello World”脚本时:

tell application "TextEdit"
    activate
end tell
tell application "System Events"
    keystroke "Hello World!"
    key code 36
end tell

我第一次运行它时,它会写“Hello World!”在 TextEdit 中应该如此。 但第二次,它写的是“Hello World!”在脚本编辑器中。 以后只写在Script Editor中。

我是否在脚本中遗漏了一些明显的东西? 或者有什么我应该关注的关于 OS X 的东西?

提前致谢!随意回复冗长​​的答案、Kbase 文章或我可能错过的其他讨论主题。

【问题讨论】:

    标签: macos applescript osx-yosemite


    【解决方案1】:
    tell application "TextEdit" to activate
    tell application "System Events"
        tell application process "TextEdit" to set frontmost to true
        keystroke "Hello World!" & return
    end tell
    

    【讨论】:

    • 您可以改进您的答案,解释它如何解决 OP 问题
    【解决方案2】:

    试试这组代码。我将在下面解释它是如何工作的,使用 cmets (--)。

    tell application "TextEdit"
    
        -- Activate TextEdit and bring it to the foreground
        activate
    
        -- Create a new document and assign it to a variable called 'myDocument'
        set myDocument to make new document
    
        -- Activate the above mentioned new document
        tell myDocument
    
            -- Add text to the above mentioned new document
            set its text to its text & "Hello, world!"
    
        end tell
    end tell
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-17
      • 2022-10-16
      • 1970-01-01
      • 2018-01-30
      • 2013-07-28
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      相关资源
      最近更新 更多