Skip to content

Bookmarks imported from chrome to safari will only add new bookmarks, but will not delete old bookmarks. This script first resets safari bookmarks to empty, and then imports bookmarks, passwords and history from chrome. You can use cron to create a task and run the script once a day to keep safari consistent with chrome.

Notifications You must be signed in to change notification settings

jiuliwei/AppleScript_to_automatically_sync_Safari_from_Chrome

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 

Repository files navigation

AppleScript_to_automatically_sync_Safari_from_Chrome

This script fixes Aneureka's sync-from-chrome-to-safari and adds new functionality to reset bookmarks.

Background

Bookmarks imported from chrome to safari will only add new bookmarks, but will not delete old bookmarks.

Solution

  • This script first resets safari bookmarks to empty, and then imports bookmarks, passwords and history from chrome.
  • I assumed the maximum number of bookmarks to delete in the root directory is 20, but you can modify it in sync-from-chrome-to-safari.scpt file.
repeat 20 times -- assume at most 20 bookmarks
	key code 125
end repeat
  • The program needs to input the password to unlock the mac. You should replace <your_password> with your own user password in sync-from-chrome-to-safari.scpt file.
-- wake the computer
do shell script "caffeinate -u -t 3"
tell application "System Events"
	keystroke "<your_password>"
	keystroke return
end tell

Automation

Because I want to automatically sync bookmarks every night, but at this point my mac's lid is closed. The cron is unusable when the lid is closed. Therefore, I decided to use launchd to do the automation.

  1. You should replace the path to sync-from-chrome-to-safari.scpt with your own path in local.sync.plist file.
<key>ProgramArguments</key> 
  <array> 
    <string>/usr/bin/osascript</string> 
    <string>the path to sync-from-chrome-to-safari.scpt</string>
  </array> 
  1. I set it to run daily at 4 am. You can change the time it runs in local.sync.plist file.
<key>StartCalendarInterval</key>
  <dict>
      <key>Hour</key>
      <integer>4</integer>
      <key>Minute</key>
      <integer>0</integer>
  </dict>
  1. Move the local.sync.plist file to /Users/<your_user_name>/Library/LaunchAgents.
  2. Run this line in the terminal to get your-user-id
id -u
  1. Then run this line to move to the LaunchAgents folder:
cd ~/Library/LaunchAgents/
  1. We can now load our agent by executing the following command:
launchctl bootstrap gui/<your-user-id> local.sync.plist
  1. Now everything is ok.
  2. If you want to modify, remember to unload the agent and then load it again:
launchctl bootout gui/<your-user-id> local.sync.plist

Tips:

Here is an example of using launchd agents to schedule scripts on macOS.

Here is an example of using cron to set up automated tasks/jobs in macOS & Linux.

About

Bookmarks imported from chrome to safari will only add new bookmarks, but will not delete old bookmarks. This script first resets safari bookmarks to empty, and then imports bookmarks, passwords and history from chrome. You can use cron to create a task and run the script once a day to keep safari consistent with chrome.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published