我有一个AppleScript,可以创建一个新的Safari文档,然后使用重复循环在该窗口中打开多个URL。我希望脚本继续使用同一窗口,即使它不是最前面的窗口也是如此。
(问题是,如果用户将焦点更改到另一个Safari窗口,则此脚本将在最前面的窗口中而不是先前创建的窗口中打开URL。)
我想通过使用来解决此问题,window id
但在脚本编写方面需要一些帮助。
为此,我创建了以下内容,但我再次认为可能存在问题。我宁愿不使用id,front window
因为用户可以在不适当的时间更改前窗,而脚本会拉错窗口的ID。
tell application "Safari"
make new document -- after this what if user changes focus
set win_ID to id of front window of application "Safari"
end tell
我宁愿使用类似
set win_ID to window id of (make new document)
即使使用上述方法,我也无法通过使用窗口的窗口ID在窗口中打开URL,并且在该脚本编写方面也需要帮助。
它的功能如下:
tell application "Safari"
open location "https://apple.com" in a new tab in window id xxxx
end tell
front window
下面的例子 的AppleScript 代码保证名单的网址就会越来越目标打开窗口,无论其位置在Z顺序的窗口。
set myURLs to {¬
"https://apple.com", ¬
"https://google.com", ¬
"https://superuser.com", ¬
"https://example.com"}
set randomNumber to random number from 1000000 to 9999999
set tmpFileName to "/private/tmp/" & randomNumber & ".html"
set tmpFileContent to "<html><head><title>" & randomNumber & "</title></head></html>"
if not my writeToFile(tmpFileContent, tmpFileName, true) then return
tell application "Safari"
make new document with properties {URL:"file://" & tmpFileName}
set i to 0
repeat while not (exists (windows whose name is randomNumber))
delay 0.1
set i to i + 1
if i = 30 then return
end repeat
set winID to (id of windows whose name is randomNumber) as number
make new tab at end of tabs of window id winID with properties {URL:item 1 of myURLs}
delete first tab of window id winID
repeat with i from 2 to (length of myURLs)
make new tab at end of tabs of window id winID with properties {URL:item i of myURLs}
delay 1
end repeat
end tell
tell application "System Events" to delete file tmpFileName
-- # Handler
on writeToFile(theData, theFile, overwriteExistingContent)
try
set theFile to theFile as string
if theFile contains "/" then
set theOpenedFile to open for access theFile with write permission
else
set theOpenedFile to open for access file theFile with write permission
end if
if overwriteExistingContent is true then set eof of theOpenedFile to 0
write theData to theOpenedFile starting at eof
close access theOpenedFile
return true
on error
try
close access file theFile
end try
return false
end try
end writeToFile
注意:根据需要滚动以查看所有代码。
笔记:
更新后的示例 AppleScript 代码包含一些错误处理,因为如果未创建tmp文件(tmpFileName
变量的值),则脚本将中止,而不会出现任何消息。这可以通过转换来改变if not my writeToFile ...
语句到全if
块,并包括一个适当的display alert
,display dialog
或display notification
命令,如想要的。
按照编码方式,此渲染--> document "Untitled"
从make new document
无关的返回,因为在包含repeat
循环的情况下,它会一直等到HTML 文件加载完毕并可以由文档中的实际名称(由文档中的<title>" & randomNumber & "</title>
标记定义)进行查询,并确保z顺序是无关紧要的。
该repeat
环路作为编码写入等待长达3秒钟的HTML 文件来加载和应大于足够的时间。必要时进行调整。
我选择放弃使用您的答案中所使用的do shell script
命令,但是,如果您更喜欢使用它,则:
代替:
if not my writeToFile(tmpFileContent, tmpFileName, true) then return
和:
set shellCMD to {"echo '", tmpFileContent, "' > '", tmpFileName, "'"} as string
do shell script shellCMD
然后从代码部分中删除on writeToFile(theData, theFile, overwriteExistingContent)
处理程序。-- # Handler
此外,如果您更喜欢使用do shell script
命令删除tmp文件,即变量的值,则:tmpFileName
代替:
tell application "System Events" to delete file tmpFileName
和:
do shell script "rm " & tmpFileName's quoted form
原始答案
下面的例子 AppleScript的 代码是如何打开一个例子列表中的网址在同一窗口中的Safari浏览器,不管它的窗口秩序。
笔记:
open location
它,因为它是Standard Additions的一部分,而不是Safari的一部分。使用URL
属性来设置URL中的文档或标签。list
的网址和指数的的列表项(URL中)列表。make new document
,在新的文件将成为front window
的Safari浏览器。获取window id
的的front window
中后,立即make new document
命令。repeat
循环的2
,并使用window id
后,将其直接确定make new document
命令被执行。set myURLs to {¬
"https://apple.com", ¬
"https://google.com", ¬
"https://superuser.com", ¬
"https://example.com"}
tell application "Safari"
make new document with properties {URL:item 1 of myURLs}
set winID to id of front window
repeat with i from 2 to (count myURLs)
make new tab at end of tabs of window id winID with properties {URL:item i of myURLs}
delay 1
end repeat
end tell
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句