我正在尝试自动化向MacOS中的Chrome添加书签的过程,因此需要一个可以发送到Mac机器的AppleScript或bash脚本才能实现此目的。
下面的vbscript适用于Windows环境。有没有办法将其转换为MacOS?
非常感谢您的评论
Imports System
Imports System.IO
Imports System.Text
Imports Microsoft.Win32
Imports System.Diagnostics
Module Module1
Dim AppPath As String
Dim PathFileVar As String
Dim TestText As String
Dim Workbench As String
Dim TriggerFound As Boolean
Dim UserProfileVar As String = Environment.GetEnvironmentVariable("UserProfile")
'Dim lineArray As New ArrayList()
Sub Main()
'MsgBox("start " & UserProfileVar)
AppPath = My.Application.Info.DirectoryPath
If My.Computer.FileSystem.FileExists(UserProfileVar & "\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.txt") Then
Try
My.Computer.FileSystem.DeleteFile(UserProfileVar & "\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.txt")
Catch ex As Exception
End Try
End If
'MsgBox("before section")
If My.Computer.FileSystem.FileExists(UserProfileVar & "\AppData\Local\Google\Chrome\User Data\Default\Bookmarks") Then
Dim lineArray As New ArrayList()
PathFileVar = UserProfileVar & "\AppData\Local\Google\Chrome\User Data\Default\bookmarks"
Dim lines() As String = IO.File.ReadAllLines(PathFileVar)
For x As Integer = 0 To lines.GetUpperBound(0)
lineArray.Add(lines(x))
Next
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(UserProfileVar & "\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.txt", True)
For x = 0 To lineArray.Count - 1
TestText = (lineArray.Item(x))
Dim dIndex = TestText.IndexOf("[")
If TriggerFound = False Then
If (dIndex > -1) Then
TriggerFound = True
'************** start inserting my bookmark ***************************
Workbench = TestText.Substring(0, dIndex)
Workbench = Workbench & "[ {"
file.WriteLine(Workbench)
file.WriteLine(" " & Chr(34) & "date_added" & Chr(34) & ": " & Chr(34) & "13116611233249308" & Chr(34) & ",")
file.WriteLine(" " & Chr(34) & "id" & Chr(34) & ": " & Chr(34) & "222" & Chr(34) & ",")
file.WriteLine(" " & Chr(34) & "name" & Chr(34) & ": " & Chr(34) & "xxxx Emp Service" & Chr(34) & ",")
file.WriteLine(" " & Chr(34) & "type" & Chr(34) & ": " & Chr(34) & "url" & Chr(34) & ",")
file.WriteLine(" " & Chr(34) & "url" & Chr(34) & ": " & Chr(34) & "https://example.com" & Chr(34))
file.WriteLine(" },")
dIndex = dIndex + 1
Workbench = TestText.Substring(dIndex)
file.WriteLine(" " & Workbench)
'***************** end of insert *************************************
Else
file.WriteLine(TestText)
End If
Else
file.WriteLine(TestText)
End If
Next
file.Close()
Try
My.Computer.FileSystem.DeleteFile(PathFileVar)
Catch ex As Exception
End Try
Threading.Thread.Sleep(500)
Try
My.Computer.FileSystem.RenameFile(UserProfileVar & "\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.txt", "Bookmarks")
Catch ex As Exception
End Try
Else
'MsgBox("file does not exist and userprofilevar " & UserProfileVar)
Try
My.Computer.FileSystem.CopyFile(AppPath & "\bookmarks", UserProfileVar & "\AppData\Local\Google\Chrome\User Data\Default\Bookmarks", overwrite:=True)
Catch ex As Exception
End Try
End If
Try
My.Computer.FileSystem.CopyFile(AppPath & "\Employee Selfservice.lnk", UserProfileVar & "\Desktop\Employee Selfservice.lnk", overwrite:=True)
Catch ex As Exception
End Try
Try
My.Computer.FileSystem.CopyDirectory(AppPath & "\_crx_bdicdcflgnjilflenkmoicnpflehicin", UserProfileVar & "\AppData\Local\Google\Chrome\User Data\Default\Web Applications\_crx_bdicdcflgnjilflenkmoicnpflehicin", True)
Catch ex As Exception
End Try
If (Not System.IO.Directory.Exists("C:\In-temp")) Then
Try
System.IO.Directory.CreateDirectory("C:\In-temp")
Catch ex As Exception
End Try
End If
Try
System.IO.File.Create("C:\In-temp\InforCRMdetect.txt").Dispose()
Catch ex As Exception
End Try
End Sub
End Module
任何帮助非常感谢,问候
由于您使用AppleScript标记了问题,因此您需要知道,为了直接在Google Chrome上使用AppleScript,您需要首先查看Google Chrome是否正在运行,然后才能尝试执行任何AppleScript代码以直接使用Google Chrome创建书签。
如果它没有运行,您必须等到谷歌Chrome的书签模型加载后再尝试使用ApplesScript添加书签。或者,如果谷歌浏览器在没有打开窗口的情况下运行,您还必须编写代码来解决这个问题,以便在没有打开窗口的情况下不会失败。
注意:因为我发布后编辑了另一个答案,我不得不更改一些内容,而不是代码。如果我的编辑中存在任何混淆,请参阅编辑历史记录。
以下示例AppleScript代码确保条件正确,因此这些方案不会发生。
set newBookmark to "https://www.google.com"
set theTitle to "Google"
set bookmarkFolder to "Bookmarks Bar"
if application "Google Chrome" is running then
tell application "Google Chrome"
tell its bookmark folder bookmarkFolder
set theResult to make new bookmark item with properties {URL:newBookmark}
set title of theResult to theTitle
end tell
end tell
else
tell application "Google Chrome"
repeat until (loading of tab 1 of window 1 is false)
delay 0.5
end repeat
tell its bookmark folder bookmarkFolder
set theResult to make new bookmark item with properties {URL:newBookmark}
set title of theResult to theTitle
end tell
quit
end tell
end if
显然你会根据需要设置newBookmark
,theTitle
和bookmarkFolder
变量的值。
在您的OP中并不完全清楚如何部署它,如果不是AppleScript .scpt或.app,已经保存在脚本编辑器中,那么您可以通过保存示例AppleScript代码将其部署为shell脚本在一个纯文本文件中,#!/usr/bin/osascript
shebang作为它的第一行,并使它与chmod
可执行。
然后可以从终端中的命令行执行它,或者只需在Finder中双击它。
另一种替代方案,如果谷歌Chrome没有运行,将直接修改用户的谷歌Chrome bookmark
文件,但AppleScript可能不是那种情况下最好的。
AppleScript使用最新版本的Sierra。
这将自动将书签直接添加到书签栏文件夹
property newBookmark : "https://stackoverflow.com" -- Insert the desired URL
property theTitle : "Whatever Title You Want"
property bookmarkFolder : "Bookmarks Bar" -- The bookmark folder where the new bookmark item will be created
tell application "Google Chrome"
try
tell active tab of window 1
repeat while loading is true
delay 0.3
end repeat
end tell
end try
tell its bookmark folder bookmarkFolder
set theResult to make new bookmark item with properties {URL:newBookmark}
set title of theResult to theTitle
end tell
end tell
这里有一些不同的旋转...如果你想将书签添加到书签栏文件夹中的书签文件夹,这将允许你从现有书签文件夹的列表中选择你想去的地方添加新书签
property newBookmark : "https://www.google.com" -- Insert the desired URL
property theTitle : "Google"
property MainBookmarkFolder : "Bookmarks Bar" -- The bookmark folder where the new bookmark item will be created
property foldersInMainBookmarkFolder : {}
tell application "Google Chrome"
try
tell active tab of window 1
repeat while loading is true
delay 0.3
end repeat
end tell
end try
set foldersInMainBookmarkFolder to title of every bookmark folder of bookmark folder MainBookmarkFolder
end tell
set chooseBookmarkFolder to (choose from list foldersInMainBookmarkFolder)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {","}
set chooseBookmarkFolder to chooseBookmarkFolder as string
set AppleScript's text item delimiters to saveTID
tell application "Google Chrome"
tell its bookmark folder MainBookmarkFolder
tell its bookmark folder chooseBookmarkFolder
set theResult to make new bookmark item with properties {URL:newBookmark}
set title of theResult to theTitle
end tell
end tell
end tell