因为自己喜欢电脑上使用Chrome浏览器,而手机端喜欢使用Safari浏览器,导致二者不能同步。所以萌生了使用脚本的方法看看能否实现自动化这个操作。目前只实现了半自动化
使用的是Applescript,我们的目的(流程)是:
- 退出Chrome,因为必须关闭Chrome,Safari才能导入Chrome的书签
- 退出Safari,如果Safari在不同的页面(其他状态)会导致模拟点击的路径不对,为了避免产生错误,先让它退出吧
- 设定一个延时,因为系统命令太快了,不设定延时的话,系统都还没把Safari退出就要让safari打开,会导致错误
- 重新打开Safari,模拟鼠标去菜单栏一步步点击相应的菜单找到导入Chrome书签的选项,点击导入按钮以及最后导入完成点击好
- 最后退出Safari
--版本一
set SecondsDelay1 to 1 --设定延迟的时间
set SecondsDelay2 to 5
tell application "Safari" --告诉safari退出
quit
end tell
tell application "Google Chrome" --告诉chrome退出
quit
end tell
delay SecondsDelay1 --等待1秒
tell application "Safari" to activate --打开safari
tell application "System Events" to tell process "Safari" --让系统时间告诉safari进程
click menu bar item "文件" of menu bar 1 of application process "Safari" of application "System Events"
click menu item "导入自" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Safari" of application "System Events"
click menu item "Google Chrome.app…" of menu "导入自" of menu item "导入自" of menu "文件" of menu bar item "文件" of menu bar 1 of application process "Safari" of application "System Events"
click button "导入" of sheet 1 of window 1 of application process "Safari" of application "System Events"
delay SecondsDelay2
click button "好" of sheet 1 of window 1 of application process "Safari" of application "System Events"
tell application "System Events"
keystroke "q" using command down
end tell
end tell
--版本二
--除去版本一的通过延迟来click最后的button"好",改为自动判断是否导入成功,成功后自动click最后的button"好"
--优化代码
set SecondsDelay1 to 0.1
tell application "Safari"
quit
tell application "Google Chrome"
quit
end tell
end tell
delay SecondsDelay1
tell application "Safari" to activate
tell application "System Events" to tell process "Safari"
click menu bar item "文件" of menu bar 1
click menu item "导入自" of menu "文件" of menu bar item "文件" of menu bar 1
click menu item "Google Chrome.app…" of menu "导入自" of menu item "导入自" of menu "文件" of menu bar item "文件" of menu bar 1
click button "导入" of sheet 1 of window 1
tell application "System Events" to tell process "Safari"
repeat until static text "完成从Google Chrome导入" of sheet 1 of window "起始页" exists
end repeat
click button "好" of sheet 1 of window "起始页"
tell application "System Events"
keystroke "q" using command down
end tell
end tell
end tell
--版本三
--增加一个延时,避免safari尚未导入成功就结束进程
set SecondsDelay1 to 0.1
set SecondsDelay2 to 2
tell application "Safari"
quit
tell application "Google Chrome"
quit
end tell
end tell
delay SecondsDelay1
tell application "Safari" to activate
tell application "System Events" to tell process "Safari"
click menu bar item "文件" of menu bar 1
click menu item "导入自" of menu "文件" of menu bar item "文件" of menu bar 1
click menu item "Google Chrome.app…" of menu "导入自" of menu item "导入自" of menu "文件" of menu bar item "文件" of menu bar 1
click button "导入" of sheet 1 of window 1
tell application "System Events" to tell process "Safari"
repeat until static text "完成从Google Chrome导入" of sheet 1 of window "起始页" exists
end repeat
click button "好" of sheet 1 of window "起始页"
delay SecondsDelay2
tell application "System Events"
keystroke "q" using command down
end tell
end tell
end tell
--版本四
--适配最新版safari
set SecondsDelay1 to 0.1
set SecondsDelay2 to 2
tell application "Safari"
quit
tell application "Google Chrome"
quit
end tell
end tell
delay SecondsDelay1
tell application "Safari" to activate
tell application "System Events" to tell process "Safari"
click menu bar item "文件" of menu bar 1
click menu item "导入自" of menu "文件" of menu bar item "文件" of menu bar 1
click menu item "Google Chrome.app…" of menu "导入自" of menu item "导入自" of menu "文件" of menu bar item "文件" of menu bar 1
click button "导入" of sheet 1 of window 1
tell application "System Events" to tell process "Safari"
repeat until static text "完成从Google Chrome导入" of sheet 1 of window "起始页" exists
end repeat
click button "好" of sheet 1 of window "起始页"
delay SecondsDelay2
tell application "System Events"
keystroke "q" using command down
end tell
end tell
end tell
其实Applescript很好理解,下面记录一下经历的几个坑
- 系统命令太快了,导致应用都没反应过来,添加一个延时
- 找对应菜单的路径,找窗口button的路径,需要使用命令下面的命令,通过返回的数据能够准确的找到路径,
eg1(范围大):
tell application "System Events" to tell process "Safari"
entire contents
end tell
eg2(缩小范围):
tell application "System Events" to tell process "Safari"
tell window 1
entire contents
end tell
end tell
- 通过上面的方法一步到位click的命令,有的时候成功,有的时候报错,实在是搞不明白,最后用了最稳的方法,一个click一条路径,一步步来就没报错了
目前还不是特别完善,最后补充:
- 如果能够静默运行就好了,就不显示窗口和界面
- 能够开机后自行运行,关机时再次运行
能够自动判断书签已导入完成,然后自动click- Applescript可以直接保存成app
Comments | NOTHING