我正在尝试编写一个Applescript来从Flickr上的(数千张)照片中恢复元数据(并将元数据复制到Apple Photos中的相同照片)。
我知道Flickr有这样做的API,但我发现Flickr API使用起来比较复杂,需要对非公开照片进行身份验证,结果需要处理以便与Apple Photos连接。
因此,我的方法是与Flickr组织用户界面进行交互,以编辑单张照片并模仿我可以手动完成的操作。这意味着单击https://www.flickr.com/photos/organize的Flickr编辑照片面板中的各种元素(具有窗口的指定边界,因此要单击的元素始终位于相同位置)。
我目前的脚本是:
copy my getMyName() to myName
tell application "Safari"
activate
-- Assume Safari is on the Flickr page www.flickr.com/photos/organize and has the edit panel for one photo open.
display dialog myName & return & return & "Read the photo metadata" buttons {"OK"}
if name of window 1 ≠ "Flickr: Organize your photos & videos" then
display dialog myName & return & return & "Name of window not 'Flickr: Organize your photos & videos'" buttons {"Quit " & myName}
else if URL of document 1 ≠ "https://www.flickr.com/photos/organize" then
display dialog myName & return & return & "URL of document not 'https://www.flickr.com/photos/organize'" buttons {"Quit " & myName} default button 2
else
if bounds of window 1 ≠ {0, 23, 1267, 789} then
set bounds of window 1 to {0, 23, 1267, 789} -- 'normalise' window size
end if
delay 2
tell application "System Events"
click at {400, 225} -- Title, Description & Tags
delay 7 -- the click takes some time!
click at {500, 275} -- Title (In Title, Description & Tags)
delay 7 -- the click takes some time!
keystroke "c" using command down -- copy
delay 2
set theTitle to the clipboard as text
click at {500, 375} -- Description (In Title, Description & Tags)
delay 7 -- the click takes some time!
keystroke "a" using command down -- select all Description
delay 2
keystroke "c" using command down -- copy
delay 2
set theDescription to the clipboard as text
click at {500, 467} -- Tags (In Title, Description & Tags)
delay 7 -- the click takes some time!
keystroke "a" using command down -- select all Description
delay 2
keystroke "c" using command down -- copy
delay 2
set theTags to the clipboard as text
display dialog "SafariScript1" & return & return & "Title: " & theTitle & return & "Description: " & theDescription & return & "Tags: " & theTags buttons {"Cancel", "OK"} default button 2
end tell
end if
end tell
on getMyName()
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
set myName to text item n of myPath
if (myName contains ".") then
set AppleScript's text item delimiters to "."
set myName to text 1 thru text item -2 of myName
end if
set AppleScript's text item delimiters to ""
return myName
end getMyName
我想在行
之前插入检查照片“编辑”面板是否已打开tell application "System Events"
Applescript代码
click at {325, 225}
点击文字的位置"编辑:"在“编辑照片”面板中,生成结果。
static text "Edit: " of group 19 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window "Flickr: Organize your photos & videos" of application process "Safari" of application "System Events"
但我无法弄清楚如何检查这是正确的结果 - 任何建议?
或者(也许更简单),手动控制单击单词"编辑"选择它,然后就可以使用
keystroke "c" using command down -- copy
要复制并检查它是"编辑"。但我找不到在AppleScript中进行控制点击的方法。有什么建议吗?
2017年2月12日更新
如果发现要检查静态文本"编辑"我可以用
if (click at {325, 225}) = static text "Edit: " of group 25 of UI element 1 ¬
of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window ¬
"Flickr: Organize your photos & videos" of application process "Safari" then
这似乎工作得很好,除了在"编辑:"之后不时出现的群号。更改(可能是因为Flickr已更改其页面代码)。在最近几天,组号从19变为20到25.由于我必须更新脚本,这有点痛苦,但我可以忍受它。
我在Applescript中更改了组号更改了类似的问题,以确保在保存时转到下一个项目?'已设置“编辑”面板的复选框。
tell checkbox 1 of group 31 of UI element 1 of scroll area 1 ¬
of group 1 of group 1 of tab group 1 of splitter group 1 ¬
of window "Flickr: Organize your photos & videos" ¬
of application process "Safari"
if not (its value as boolean) then click it
delay theDelay -- the click takes some time!
end tell
我现在有一个有效的AppleScript(只要我不断更新组号)。
2017年2月12日进一步更新
找到一种忽略组号更改的方法。 点击{x,y} 会在{x,y}处显示UI元素的路径。这可以复制到变量中。然后,该变量可用于访问UI元素的属性。因此,对于我感兴趣的Flickr页面上的Flickr编辑面板,要检查面板是否打开,可以对静态文本进行检查"编辑:"在正确的地方,无需通过以下方式了解组号:
copy (click at {325, 225}) to UIelement
if (class of UIelement = static text) and (((value of UIelement) as text) = "Edit: ") then
同样,为了确保在保存时转到下一个项目?'复选框已设置:
copy (click at {453, 541}) to checkboxGotoNext
tell checkboxGotoNext
if not (its value as boolean) then click it
delay theDelay -- the click takes some time!
end tell