在不离开当前程序的情况下与 运行 Applescript script/program 交互(热键、发送的击键等)
Interact WITH a running Applescript script/program without leaving the current program (Hotkey, sent keystroke etc)
我的具体问题的一般问题是如何在不离开您当前所在的程序的情况下与 运行ning 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") 并制作另一个脚本以在 运行ning 时向其发送 "enter" 击键(然后通过一些激活新脚本一种热键):
tell application "System Events"
tell application "hent2" to activate
key code 36
end tell
它什么都不做。
关于如何进行的任何建议?使用 "sent keystroke to Applescript" 还是更优雅的解决方案?
编辑:
我开始使用下面 Jerry 提供的 "Hitting a button" 解决方案。
但是我无法让处理程序解决方案工作。它清楚地与主脚本通信,因为它告诉我它找不到 url 列表 "listen":
"error "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
点击按钮
首先,在脚本对话框中,如果你想按下一个按钮,使用系统事件的click button
:
tell application "System Events"
tell application process "hent2"
tell window 1
click button "Ja"
end tell
end tell
end tell
即使被命中的进程不是最前面的,它也能工作,所以它可能就是您所需要的。
通过处理程序激活
您还可以通过 tell
在另一个 AppleScript 中激活一个 AppleScript 中的处理程序。例如,将其另存为 Test Handler
、Application 和 Stay Open After 运行:
on ContinueOnward()
display notification "Continuing Onward"
end ContinueOnward
然后,在脚本编辑器中,编写以下脚本:
tell application "Test Handler"
ContinueOnward()
end tell
当您 运行 该脚本时,您应该会看到一条通知弹出,提示“继续前进”。当然,在您自己的脚本中,您可以使用该处理程序转到下一个 URL.
为了跨不同的处理程序维护变量,您需要使用 properties 或 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
在您的情况下,您需要使 i
、listen
和 lengde
成为全局变量。放:
global i, listen, lengde
在你的处理程序的顶部,就像我在示例 ContinueOnward 处理程序的顶部对 currentCount
所做的那样。
我的具体问题的一般问题是如何在不离开您当前所在的程序的情况下与 运行ning 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") 并制作另一个脚本以在 运行ning 时向其发送 "enter" 击键(然后通过一些激活新脚本一种热键):
tell application "System Events"
tell application "hent2" to activate
key code 36
end tell
它什么都不做。
关于如何进行的任何建议?使用 "sent keystroke to Applescript" 还是更优雅的解决方案?
编辑:
我开始使用下面 Jerry 提供的 "Hitting a button" 解决方案。
但是我无法让处理程序解决方案工作。它清楚地与主脚本通信,因为它告诉我它找不到 url 列表 "listen":
"error "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
点击按钮
首先,在脚本对话框中,如果你想按下一个按钮,使用系统事件的click button
:
tell application "System Events"
tell application process "hent2"
tell window 1
click button "Ja"
end tell
end tell
end tell
即使被命中的进程不是最前面的,它也能工作,所以它可能就是您所需要的。
通过处理程序激活
您还可以通过 tell
在另一个 AppleScript 中激活一个 AppleScript 中的处理程序。例如,将其另存为 Test Handler
、Application 和 Stay Open After 运行:
on ContinueOnward()
display notification "Continuing Onward"
end ContinueOnward
然后,在脚本编辑器中,编写以下脚本:
tell application "Test Handler"
ContinueOnward()
end tell
当您 运行 该脚本时,您应该会看到一条通知弹出,提示“继续前进”。当然,在您自己的脚本中,您可以使用该处理程序转到下一个 URL.
为了跨不同的处理程序维护变量,您需要使用 properties 或 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
在您的情况下,您需要使 i
、listen
和 lengde
成为全局变量。放:
global i, listen, lengde
在你的处理程序的顶部,就像我在示例 ContinueOnward 处理程序的顶部对 currentCount
所做的那样。