如何正确将嵌套列表从Applescript传递到Bash

我正在从事一个项目,该项目涉及使用Applescript在用户Google Chrome浏览器中列出打开的URL,以便在我最终要重新打开Chrome时保存它们。发现我需要一种方法来确定哪个窗口中存在哪些选项卡,所以我决定尝试在Applescript中创建一个嵌套列表,其中每个窗口都是其自身的选项卡URL子列表,然后在声明的变量中返回它在子外壳之前。

我这样做的方法是通过以下代码

tabs=$(/usr/bin/osascript << EOT
    tell application "Google Chrome"
        # save a variable to the number of windows open     
        set windowCount to number of windows        
        set myWindows to {}

        repeat with x from 1 to windowCount     
            # count the tabs in the window we are iterated upon     
            set tabcount to number of tabs in window x

            # this list will hold the URLs,delimited by commas         
            set myURLs to {}

            # secondary loop,this time iterating tabs
            repeat with y from 1 to tabcount            
                # grab URL from current tab
                set tabURL to URL of tab y of window x

                # append URL to end of list         
                copy tabURL to the end of myURLs
            end repeat

            # this means our end result will be a list of lists
            # containing the URLs of all the tabs of all the windows
            copy myURLs to the end of myWindows             
        end repeat

        return myWindows
    end tell
    EOT)

我遇到的问题是,尽管Applescript正在正确构建嵌套列表,即

{{"https://stackoverflow.com/questions/ask"},{"https://twitter.com","https://facebook.com"}}

当我稍后引用$tabs时,Bash似乎使列表变平了

https://stackoverflow.com/questions/ask,https://twitter.com,https://facebook.com

这使我相信在传递复杂变量时Applescript和Bash不会相处。是这种情况,并且Bash无法从Applescript读取多维列表吗?还是我只是对此编程不正确?

谢谢!

xingwei78 回答:如何正确将嵌套列表从Applescript传递到Bash

这是一个基本的准系统脚本,其中包含两个处理程序:storeURLs()restoreURLs()

storeURLs()检索当前打开的Chrome标签页的嵌套列表(按窗口分组),并将其网址写出到文件中。 restoreURLs()读取此文件并在Chrome中打开URL,以恢复标签和窗口的排列。

以当前形式运行脚本会将URL列表存储在文件 ~/.chrometabs 中。要使脚本恢复已保存的URL,请在第一个"storeURLs()"(第6行)的前面加上两个连字符(--)或哈希(#),以注释掉该脚本。并通过删除"restoreURLs()"之前的两个连字符取消注释下面的行。

property home : system attribute "HOME"
property file : home & "/.chrometabs" --> /Users/%YOU%/.chrometabs


on run
    storeURLs()
    --restoreURLs()
end run

on storeURLs()
    close access (open for access my file)

    tell application id "com.google.chrome" to set ¬
        URLs to the URL of every tab in every window

    write the URLs to my file
    return the URLs
end storeURLs

to restoreURLs()
    close access (open for access my file)
    if (get eof of my file) = 0 then return false
    set URLs to read my file as list

    tell application id "com.google.chrome"
        repeat with URLgroup in the reverse of URLs
            tell (make new window)
                set the active tab's URL to the URLgroup's 1st item
                repeat with |URL| in the rest of the URLgroup
                    make new tab with properties {URL:|URL|}
                end repeat
            end tell
        end repeat

        activate
    end tell

    return the URLs
end restoreURLs

该脚本只包含最基本的错误预防功能,因此将从其他错误处理中受益。它是使用 macOS 10.12.6 中的 AppleScript 2.5版编写和测试的。我预计在最新版本的macOS(Mojave,Catalina)中,唯一潜在的问题是安全漏洞,由于其位置或位置,该安全漏洞可能阻止脚本创建和/或写入文件路径~/.chrometabs。文件名中的前导"."表示在macOS中是不可见的文件。

本文链接:https://www.f2er.com/3136147.html

大家都在问