我在"自动操作"中使用AppleScript来复制页面的源代码并将其保存到文件中。出于某种原因,当我在 posix 路径中使用变量 (titleVal( 时,循环中的其余代码将被忽略,包括永远不会写入的文件。
我之前用我的完整AppleScript更新了代码,以防它与我以前的几行有关。我正在按以下顺序将"自动操作"与指定的 Finder 项目一起使用:"urlList.txt"和 fileList.txt"。
on run {input, parameters}
set updateCount to 0
read (item 1 of input)
set ps to paragraphs of the result
set tot to count ps
set TLFile to (("Users:Admin:Desktop:download captions:") as text) & "fileList.txt"
set TLLines to paragraphs of (read file TLFile as «class utf8»)
tell application "Safari"
reopen
activate
end tell
repeat with i from 1 to tot
set p to item i of ps
if p is not "" then
try
tell application "Safari"
tell front window
set r to make new tab with properties {URL:"https://www.youtube.com/timedtext_editor?v=" & p & "&lang=en&name=&kind=&contributor_id=0&bl=vmp&action_view_track=1"}
set current tab to r
set titleVal to item i of TLLines
set updateCount to updateCount + 1
do shell script "echo The value: " & updateCount
delay 2
do JavaScript "document.getElementById('movie_player').outerHTML = ''" in current tab
do JavaScript "document.getElementById('creator-page-sidebar').outerHTML = ''" in current tab
do JavaScript "document.getElementById('footer').outerHTML = ''" in current tab
delay 3
do JavaScript "document.getElementsByClassName('yt-uix-button yt-uix-button-size-default yt-uix-button-default action-track-button flip yt-uix-menu-trigger')[0].click()" in current tab
delay 1
do JavaScript "document.getElementById('aria-menu-id-2').getElementsByTagName('ul')[0].getElementsByTagName('li')[5].getElementsByTagName('a')[0].click()" in current tab
delay 4
-- using a variable in path1 is where it screws up. try changing it to another variable value and it will have the same effect.
set myString to source of current tab
set path1 to "/Users/Admin/Desktop/download captions/downloadedCaptions/" & titleVal & ".srt"
say path1
set newFile to POSIX file path1
--set newFile to POSIX file "/Users/Admin/Desktop/download captions/downloadedCaptions/test.xml.srt"
open for access newFile with write permission
write myString to newFile
close access newFile
-- i have exit repeat here to only test the first loop
exit repeat
end tell
end tell
end try
end if
end repeat
end run
没有变量就可以正常工作,但我需要变量使脚本在循环中正常工作。我已经检查了变量的值。我还尝试了"和引用形式的titleVal &"。
更新:当我按照建议删除尝试/结束尝试以获取错误时,错误是:操作"运行AppleScript"遇到错误:"Safari 出现错误:无法获取窗口 1 的 POSIX 文件"/用户/管理员/桌面/下载字幕/下载字幕/测试.srt"。
发生此错误是因为您要将文件写入无法工作的 Safari tell window
块中。
我建议使用单独的处理程序。将on writeFile
处理程序放在on run
处理程序之外。我添加了可靠的错误处理,数据以 UTF-8 编码保存。
on writeFile(theData, fileName)
set newFile to "/Users/Admin/Desktop/download captions/downloadedCaptions/" & fileName & ".srt"
try
set fileDescriptor to open for access newFile with write permission
write theData to fileDescriptor as «class utf8»
close access fileDescriptor
on error
try
close access newFile
end try
end try
end writeFile
并调用它(从delay 4
到末尾替换代码的一部分(
delay 4
-- using a variable in path1 is where it screws up. try changing it to another variable value and it will have the same effect.
set myString to source of current tab
my writeFile(myString, titleVal)
exit repeat
end tell
end tell
end try
end if
end repeat
end run
通过更改一行很容易解决此问题:
set newFile to POSIX file path1
自:
set newFile to (path1 as POSIX file)
但我不知道为什么。似乎是一个错误,因为它在没有变量的情况下工作。请在评论中提供任何详细信息,为什么这适用于原始设置的 newFile 行。