我正在制作一个脚本,它更新我的macossafari上的书签,使我所有订阅的子版块在一个特定的文件夹中作为单独的书签。在Python中,我已经将所有的subreddits作为元组的排序列表,第一个元素是想要的书签名称,第二个元素是bookmark url:
bookmarks = [
('r/Android', 'https://www.reddit.com/r/Android/'),
('r/Apple', 'https://www.reddit.com/r/Apple/'),
('r/Mac', 'https://www.reddit.com/r/Mac/'),
('r/ProgrammerHumor', 'https://www.reddit.com/r/ProgrammerHumor/')
]
如何清除Safari中的subreddit bookmarks文件夹并在该文件夹中创建这些新书签?在
到目前为止,我一直使用Python,但是从Python程序调用外部AppleScript或Shell脚本没有问题。在
下面是想要的结果的图像,每个书签都链接到各自的subreddit url:
.py
文件调用它。实现这一点所需的所有工具都内置在macOS上。在重要提示:使用macOS Mojave(10.14.x)
+
您需要执行下面“macOS Mojave限制”部分中的步骤1-10。这些更改允许对Bookmarks.plist
进行修改。在继续之前,请创建
Bookmarks.plist
的副本,该副本位于~/Library/Safari/Bookmarks.plist
。您可以运行以下命令将其复制到您的桌面:要在以后还原
^{pr2}$Bookmarks.plist
,请运行:属性列表
MacOS有内置的属性列表(} ,和^{} ,它们有助于编辑通常包含平面数据结构的应用程序首选项。然而,Safari的
.plist
)相关的命令行工具,即^{Bookmarks.plist
有一个深度嵌套的结构,这两个工具都不擅长编辑。在将
.plist
文件转换为XML
^{3}$plutil
提供了一个-convert
选项来将.plist
从二进制转换为XML。例如:类似地,以下命令将转换为二进制:
转换为XML可以使用XSLT,这是转换复杂XML结构的理想选择。在
使用XSLT样式表
此自定义XSLT样式表转换
Bookmarks.plist
添加元素节点以创建书签:模板.xsl
运行转换:
此
.xsl
需要指定每个所需书签属性的参数。在首先确保
Bookmarks.plits
是XML格式的:利用内置的^{} 将
template.xsl
应用到Bookmarks.plist
。在首先,^{} 到
template.xsl
所在的位置,然后运行以下复合命令:这将在您的
Desktop
上创建result-plist.xml
,其中包含一个名为QUUX
的新书签文件夹,其中包含两个新书签。让我们进一步了解上述复合命令中的每个部分:
^{} 生成新的
Bookmarks.plist
中需要的三个UUID(一个用于文件夹,一个用于每个书签条目)。我们预先生成它们并将它们传递给XSLT,因为:xsltproc
需要XSLT1.0xsltproc
的stringparam
选项表示如下自定义参数:stringparam bkmarks-folder <value>
-书签文件夹的名称。在{cd33}每个书签的属性。在
每个书签规范都用逗号(
,
)分隔。每个分隔字符串有三个值:书签名称、URL和GUID。这3个值以空格分隔。stringparam guid <value>
-书签文件夹的GUID。最后一部分:
定义到的路径;
.xsl
、源XML和目标。要计算刚刚发生的转换,请使用^{} 来显示两个文件之间的差异。例如运行:
然后按几次F键向前导航到每一页,直到在两列中间看到
>
符号-它们指示添加新元素节点的位置。按B键向后移动一页,然后键入Q退出diff.使用Bash脚本
我们现在可以在Bash脚本中使用前面提到的
.xsl
。在脚本.sh
解释
script.sh
提供以下功能:.plist
未损坏。在template.xsl
内联,通过xsltproc
转换{.plist
转换为XML,然后再转换回二进制。在Bookmarks.plist
目录,有效地替换原始文件。在运行shell脚本
cd
到script.sh
所在的位置,并运行以下^{script.sh
可执行:运行以下命令:
然后将以下内容打印到CLI:
Safari现在有一个名为
stackOverflow
的书签文件夹,其中包含两个书签(bash
和python
)。使用Python脚本
有两种方法可以通过
.py
文件执行script.sh
。在方法A:外部shell脚本
下面的
.py
文件执行外部script.sh
文件。让我们将文件命名为create-safari-bookmarks.py
,并将其与script.sh
保存在同一个文件夹中。在创建safari-书签.py
说明:
第一个
def
语句定义了一个run-script
函数。它有两个参数:folder_name
和bkmarks
。subprocess
模块call
方法本质上使用所需参数执行script.sh
。第二条
def
语句定义了一个tuple_to_shell_arg
函数。它有一个参数tup
。Stringjoin()
方法将元组列表转换为script.sh
所需的格式。它本质上转换元组列表,例如:并返回一个字符串:
按如下方式调用
run_script
函数:这将传递两个参数;
subreddit
(bookmarks文件夹的名称)和每个所需书签的规范(格式如前面第2点所述)。正在运行
create-safari-bookmarks.py
使
create-safari-bookmarks.py
可执行:然后调用它:
方法B:内联shell脚本
根据具体的用例,您可能需要考虑在
.py
文件中内联script.sh
,而不是调用外部.sh
文件。让我们将这个文件命名为create-safari-bookmarks-inlined.py
,并将其保存到create-safari-bookmarks.py
所在的同一个目录中。在重要:
您需要将
script.sh
中的所有内容复制并粘贴到create-safari-bookmarks-inlined.py
中指定的位置。将其粘贴到
bash_script = """\
部分后面的下一行。create-safari-bookmarks-inlined.py
中的"""
部分应该在粘贴的script.sh
内容的最后一行后面的自己的行上。在当在}的第31行应该如下所示:
.py
中内联时,script.sh
的第31行必须使用另一个反斜杠转义'%s\0'
部分(\0
是空字符),即{这一行可能在
create-safari-bookmarks-inlined.py
中的第37行。创建safari书签-内联.py
说明
此文件的结果与
create-safari-bookmarks.py
相同。这个修改过的
.py
脚本包含一个修改后的run_script
函数,该函数利用Python的tempfile
模块将内联shell脚本保存到临时文件中。Python的
subprocess
模块call
方法随后执行临时创建的shell文件。正在运行
create-safari-bookmarks-inlined.py
使
create-safari-bookmarks-inlined.py
可执行:然后通过运行以下命令调用它:
附加说明:将书签附加到现有文件夹
目前,每次再次运行上述脚本/命令时,我们都在有效地替换任何现有的命名Safari bookmark folder(它与给定的bookmark folder同名),使用一个全新的bookmark folder创建指定的书签。在
但是,如果您想将书签附加到现有文件夹,则
template.xsl
包含一个要传递给它的附加参数/参数。请注意第14行的部分内容:它的默认值是
false
。所以,如果我们要将run_script
中的run_script
函数改为如下。在也就是说,添加一个名为
keep_existing
的第三个参数,并在subprocess.call([...])
中包含对它的引用,即将其作为第三个参数传递给script.sh
(…并随后传递到XSLT样式表)。在{{cd110>然后我们可以调用一个额外的{cd110}函数
- 从Apple菜单中选择系统首选项。在
- 在系统首选项窗口中,单击安全和策略图标。在
- 在安全和策略窗格中,单击隐私选项卡。在
- 在左侧栏中选择完整磁盘访问。在
- 单击左下角的锁定图标以允许更改。在
- 输入管理员密码,然后单击解锁按钮。在
- 接下来单击加号图标(+)。在
- 选择终端.app,可位于
- 终端.app将被添加到列表中。在
- 单击锁定图标以阻止任何进一步的更改,然后退出系统首选项。在
但是,进行上述更改(即传入
"true"
以保留现有书签),确实有可能导致创建重复的书签。例如,当我们有一个退出的书签(名称和URL)时,就会出现重复的书签,然后在以后用相同的名称和URL重新提供该书签。在限制:当前为书签提供的任何名称参数都不能包含空格字符,因为它们被脚本用作分隔符。在
马科斯莫哈韦限制
由于macOS上更严格的安全策略,默认情况下不允许访问
~/Library/Safari/Bookmarks.plist
(如this answer中所述)。在因此,有必要授予终端.app(或其他首选的CLI工具,如iTerm)访问整个磁盘。为此,您需要:
/Applications/Utilities/
,然后单击打开按钮。在{a10}
我从来没有在Safari(而不是AS字典)中找到管理书签的AS命令。所以我建立了自己的例程来处理Safari bookmark plist文件。然而,它们会受到苹果公司在未来书签处理方式上的意外变化的影响!到目前为止,它仍然在工作,但我还没有使用10.14
首先,您必须获取这个plist文件才能更改它。这个部分必须在你的主代码中。它为您提供了plist文件的修补程序:
这里有两个子例程来管理书签。第一个检查书签是否存在
^{pr2}$您可以将此处理程序称为如下:
^{3}$第二个处理程序创建一个新书签:
我使用这些程序来添加、检查和更改书签右侧的书签。在您的例子中,您需要使用书签子菜单,然后您必须调整此代码,但主要概念是相同的。在
为了方便起见,我建议您开始查找plist文件(Library/Safari/书签.plist)当你的书签在子菜单中时,可以看到它的结构。在
我希望这有帮助!在
相关问题 更多 >
编程相关推荐