Write an automation for mac to send all new Apple Notes to a REST API
- User
Write an automation for mac to send all new Apple Notes to a REST API
- Assistant
Here's an example of how to automate sending all new Apple Notes to a REST API using AppleScript on a Mac:
- Open the Script Editor app on your Mac.
- Click on "File" > "New" to create a new script.
- In the script editor, enter the following AppleScript code:
tell application "Notes" set allNotes to notes repeat with aNote in allNotes if modification date of aNote is greater than date "2021-09-01" then set noteTitle to name of aNote set noteBody to body of aNote set apiUrl to "https://your-rest-api.com/notes" tell application "curl" set postData to "-d '{\"title\":\"" & noteTitle & "\", \"body\":\"" & noteBody & "\"}'" do shell script "curl -X POST -H 'Content-Type: application/json' -d '" & postData & "' " & apiUrl end tell end if end repeat end tell - User
How could this be triggered automatically?
- Assistant
One way to trigger the "SendNotesToRestApi" application automatically is to use macOS's built-in scheduling feature, called "launchd". Here's how to do it:
- Open the Terminal app on your Mac.
- Type the following command and press Enter to create a new launchd property list file:
nano ~/Library/LaunchAgents/com.sendnotes.plist- Paste the following XML code into the file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.sendnotes</string> <key>ProgramArguments</key> <array> <string>/Applications/SendNotesToRestApi.app/Contents/MacOS/SendNotesToRestApi</string> </array> <key>StartInterval</key> <integer>3600</integer> </dict> </plist>- Press Control + X, then Y, then Enter to save and exit nano.
- Type the following command and press Enter to load the launchd property list:
- User
How can I package all of the above into a downloadable file the user can install?
- Assistant
To package the above automation into a downloadable file, you can create a macOS application bundle. Here's how to do it:
- Create a new folder on your Mac and name it something like "SendNotesToRestApi".
- Inside the folder, create a new folder called "Contents".
- Inside the "Contents" folder, create a new folder called "MacOS".
- Move the "SendNotesToRestApi" application you created earlier to the "MacOS" folder.
- Create a new text file inside the "Contents" folder called "Info.plist".
- Paste the following XML code into the "Info.plist" file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleExecutable</key> <string>SendNotesToRestApi</string> <key>CFBundleName</key> <string>SendNotesToRestApi</string> <key>CFBundleVersion</key> <string>1.0</string> <key>LSUIElement</key> <true/> </dict> </plist> - User
Is it possible to make it into an executable?
- Assistant
Yes, it's possible to create an executable file from the application bundle so that the user can simply double-click it to run the automation. Here's how to do it:
- Open the Terminal app on your Mac.
- Type the following command and press Enter to navigate to the "MacOS" folder inside the application bundle:
cd /path/to/SendNotesToRestApi/Contents/MacOSReplace "/path/to/SendNotesToRestApi" with the actual path to the folder you created in step 1.
- Type the following command and press Enter to make the "SendNotesToRestApi" file executable:
chmod +x SendNotesToRestApi- Type the following command and press Enter to create a symlink to the "SendNotesToRestApi" file with the same name as the application bundle:
ln -s SendNotesToRestApi /path/to/SendNotesToRestApi.appReplace "/path/to/SendNotesToRestApi" with the actual path to the folder you created in step 1.
- Close the Terminal app.
- Control-click on the "SendNotesToRestApi.app" file and choose "Compress" to create a ZIP file of the application bundle.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.