如何通过AppleScript仅在未找到目标URL时打开该URL?
修正Google Keep标签页检测逻辑
我懂你的痛点——当前脚本的问题在于它会逐个遍历每个标签页,只要遇到非Keep的标签就触发打开操作,这会导致重复打开多个Keep页面,完全不符合「仅在无目标URL时打开」的需求。咱们调整一下逻辑,先全局检查是否已经存在Keep标签页,再决定是否新建:
tell application "Google Chrome" set keepTabExists to false -- 先检查Chrome是否在运行,再遍历所有标签页 if it is running then repeat with w in windows repeat with t in tabs of w if URL of t starts with "https://keep.google.com/" then set keepTabExists to true -- 找到目标标签后直接激活该标签和窗口,然后退出循环 set active tab of w to t activate w exit repeat end if end repeat if keepTabExists then exit repeat end repeat -- 如果没找到Keep标签页,再新建 if not keepTabExists then open location "https://keep.google.com/" delay 1 activate end if else -- Chrome没运行的话,直接启动并打开Keep activate open location "https://keep.google.com/" delay 1 activate end if end tell
关键修改点:
- 新增
keepTabExists布尔变量,用来标记是否已存在Keep标签页,避免重复判断 - 嵌套遍历窗口和标签页,找到目标标签后直接激活对应窗口和标签,然后退出所有循环,不用继续遍历
- 只有在确认所有标签页都没有Keep页面的情况下,才执行新建操作
- 简化了重复的
activate调用,逻辑更清晰
内容的提问来源于stack exchange,提问作者Canato
相关产品推荐
相关产品推荐

