在不离开当前程序的情况下与正在运行的Applescript脚本/程序交互(热键,发送击键等)

时间:2015-12-10 09:37:57

标签: applescript

我的具体问题的一般问题是如何在不离开您当前所在的程序的情况下与正在运行的Applescript进行交互。这与如何使用Applescript与其他程序进行交互相反。

我需要浏览大量网页并将信息输入电子表格。我使用Applescript对该过程进行了一些自动化处理,该Applescript将网页作为输入,然后逐个打开其相关链接,等待对话框中的单击,然后继续进行下一步。

如果要运行脚本,请使用http://www.resultat.dk/tennis/challenger-singler/champaign/resultater/作为对话框的输入

--Let the user input the the main webpage
display dialog "Linkzor" default answer ""
set tunering to text returned of result

--get the javascript of the web page
tell application "Safari"

    tell window 1
        open location tunering
    end tell
    delay 5
    set listen to "" as string
    tell front document to set ¬
        startText to {name, do JavaScript "window.document.documentElement.outerHTML"}
end tell
set listen to {}
set teksten to startText as string

--Gets all the relevant link variables of the web page
repeat with i from 1 to 500

    set forekomst to text (nthOffset(teksten, "g_2_", i) + 4) thru (nthOffset(teksten, "g_2_", i) + 11) of teksten
    if nthOffset(teksten, "g_2_", i) = 0 then exit repeat

    set punkt to "http://www.resultat.dk/kamp/" & forekomst & "/#kampreferat"
    set listen to listen & punkt
end repeat

set lengde to count of listen

--opens the links one by one. 
repeat with x from 1 to lengde

    tell application "Safari"
        tell window 1
            set URL of document 1 to item x of listen
        end tell
    end tell

    --THIS IS THE PART I WANT TO INTERACT WITH FROM OUTSIDE THE SCRIPT
    set question to display dialog "forsæt?" buttons {"Ja", "nej"} default button 1
    set answer to button returned of question
    if answer is "nej" then exit repeat

end repeat




on nthOffset(myText, subString, n)
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to subString
    -- There will be one more text item than there are instances of the      substring in the string, so:
    if (count myText's text items) > n then
        -- There are at least n instances of the substring in the text.
        -- The first character of the nth instance comes immediately after the last
        -- character of the nth text item.
        set o to (count text from text item 1 to text item n of myText) + 1
    else
        -- There isn't an nth instance of the substring in this text.
        set o to 0
    end if
    set AppleScript's text item delimiters to astid
    return o
end nthOffset

现在我想进一步自动化,所以当进入下一个网页时,我不必离开电子表格点击对话框。有没有办法强制Applescript继续没有我实际点击对话框?我曾尝试将脚本保存为应用程序(“hent2”)并制作另一个脚本,以便在它运行时向其发送“输入”按键(然后通过某种热键激活新脚本):

tell application "System Events"
    tell application "hent2" to activate
    key code 36
end tell

它没有做任何事情。

有关如何进行的任何建议?使用“发送按键到Applescript”还是更优雅的解决方案?

编辑:

我使用下面的Jerry的“按下按钮”解决方案。

但是我无法让处理程序解决方案起作用。它清楚地与主脚本通信,因为它告诉我它找不到url的列表“listen”:

“错误”hent3收到错误:未定义变量listen。“数字-2753来自”listen“”

当我将脚本保存并作为程序运行时,我在自动运行的部件中定义并分配值为“listen”。但是当我从另一个Applescript调用子程序时,它对“listen”

一无所知

这是我的尝试:

--Let the user input the the main webpage
display dialog "Linkzor" default answer ""
set tunering to text returned of result

--get the javascript of the web page
tell application "Safari"

    tell window 1
        open location tunering
    end tell
    delay 5
    tell front document to set ¬
        startText to {name, do JavaScript "window.document.documentElement.outerHTML"}
end tell
set listen to {} --HERE I DEFINE THE VARIABLE
set teksten to startText as string

--Gets all the relevant link variables of the web page
repeat with i from 1 to 500

    set forekomst to text (nthOffset(teksten, "g_2_", i) + 4) thru (nthOffset(teksten, "g_2_", i) + 11) of teksten
    if nthOffset(teksten, "g_2_", i) = 0 then exit repeat

    set punkt to "http://www.resultat.dk/kamp/" & forekomst & "/#kampreferat"
    set listen to listen & punkt -HERE I ASSIGN VALUES TO IT
end repeat

set lengde to count of listen
set i to 1

on ContinueOnward()
    tell application "Safari"
        tell window 1
            set URL of document 1 to item i of listen --HERE IT ISN`T RECOGNISED
        end tell
    end tell
    set i to i + 1
    if i > lengde then
        display notification "slut"
    end if
end ContinueOnward

on nthOffset(myText, subString, n)
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to subString
    -- There will be one more text item than there are instances of the substring in the string, so:
    if (count myText's text items) > n then
        -- There are at least n instances of the substring in the text.
        -- The first character of the nth instance comes immediately after the last
        -- character of the nth text item.
        set o to (count text from text item 1 to text item n of myText) + 1
    else
        -- There isn't an nth instance of the substring in this text.
        set o to 0
    end if
    set AppleScript's text item delimiters to astid
    return o
end nthOffset

1 个答案:

答案 0 :(得分:2)

点击按钮

首先,在编写脚本对话框时,如果要按一个按钮,请使用系统事件click button

tell application "System Events"
    tell application process "hent2"
        tell window 1
            click button "Ja"
        end tell
    end tell
end tell

即使被击中的过程不是最前面的,所以它可能就是你需要的所有内容。

通过处理程序

激活

您还可以通过另一个AppleScript中的tell在一个AppleScript中激活处理程序。例如,将其另存为Test Handler应用运行后保持打开

on ContinueOnward()
    display notification "Continuing Onward"
end ContinueOnward

然后,在脚本编辑器中,编写以下脚本:

tell application "Test Handler"
    ContinueOnward()
end tell

当您运行该脚本时,您应该会看到弹出的通知“继续向前”。当然,在您自己的脚本中,您可以使用该处理程序转到下一个URL。

为了在不同的处理程序中维护变量,您需要使用属性全局或某些组合。属性在应用程序的运行中保持其值; globals仅在应用程序的一次运行中保持其值。在我阅读您的需求时,您将需要全局变量。在需要访问全局的每个处理程序中,使用:

global variablename

您还需要在处理程序之外初始化变量。

例如,此应用程序(另存为应用程序,保持打开状态)将更改其通知消息,然后在第四次之后退出:

set currentCount to 0

on ContinueOnward()
    global currentCount
    set currentCount to currentCount + 1

    if currentCount < 5 then
        display notification "Continuing with item " & currentCount
    else
        display notification "Quitting"
        quit
    end if
end ContinueOnward

在您的情况下,您需要将ilistenlengde设为全局变量。放:

global i, listen, lengde

位于处理程序的顶部,就像我在示例 ContinueOnward 处理程序顶部的currentCount所做的那样。