Pages

Monday, December 18

Deleting Duplicate Notes in Notes.app using AppleScript

I found myself digging through my Notes.app the other day and, lo and behold, there was a whole bunch of duplicate notes hanging around. Pretty sure I goofed and imported them twice or something along those lines.

To fix the mess, I whipped up a quick AppleScript that zapped the duplicates just by looking at the note titles. Since the folder wasn't a big mess and I was dealing with a pretty neat pile, I was cool with the script going on a deleting spree. Just to be safe, though, I did a quick sweep through the "Recently Deleted" folder afterward to make sure nothing important got caught in the crossfire.

Use with caution:

tell application "Notes"

set targetFolder to "Target" -- Change this to your folder name

if not (exists folder targetFolder) then

display alert "Folder not found"

return

end if

set theNotes to notes of folder targetFolder

set noteNames to {}

set duplicates to {}

repeat with aNote in theNotes

set noteName to name of aNote

if noteNames contains noteName then

set end of duplicates to aNote

else

set end of noteNames to noteName

end if

end repeat

repeat with duplicateNote in duplicates

delete duplicateNote

end repeat

if length of duplicates is 0 then

display dialog "No duplicates found"

else

display dialog (length of duplicates) & " duplicates deleted."

end if

end tell




Please leave comments below.

Thursday, December 7

Grabbing a domain name out of a URL using Shortcuts

Recently I needed the ability to grab a domain name (specifically the hostname) out of a URL using Shortcuts.  

I wanted to integrate this functionality into a much larger shortcut, but this blog post will highlight just the domain parsing bit.


This will get you the whole domain name from any given URL from the share sheet.  That being said, I wanted to take it a step further and get just the hostname out of the domain name:


Assuming that there is more than two parts to the domain name, this works.  But if you have and "A" type of url "blah.com" instead of a "CNAME" type like: "www.blah.com", you're going to get "com" as a result.  So your milage may vary.

I couldn't find a good way to do this on the Internet, despite several attempts for people to try and figure it out using regex.  You can then go on to store the result as a variable and then use the variable anywhere else in the rest of the Shortcut.  

Anyway, here's a way to do it.  Hopefully this blog post helps someone trying to figure out the same issue.  It's a little hacky, but knock yourself out.


Please leave comments below.

Sort emails by year, using AppleScript

So, way back when I was using Thunderbird, I had this awesome plugin that organized my archived emails by year. It was super handy for finding stuff or just taking a trip down memory lane.

But when I switched to Mail.app, I couldn't find a similar plugin that I liked. Plus, even if one existed, I'd have to install it on every new Mac I got. So, I decided to take matters into my own hands and whip up some AppleScript magic.

In the code, you can pick a folder (sourceFolder) - in my case, it's "Recovered". You also need to specify your account name, like "iCloud" in my code. If your account goes by a different name, like "Exchange" or "Gmail", just tweak that part.

Then there's this variable called "yearsMailbox" at the top of the code, (in my case "Years") which is basically the folder where the emails will end up based on their years. You can rename it if you're feeling fancy.

You can probably make this more efficient by having it map the year folders one time instead of checking every time it wants to move a message, but I feel you're just playing "AppleScript golf" at that point.  This works.  It works great.  Moving on.

tell application "Mail"

-- Specify the parent mailbox where the year folders are located

set yearsMailbox to mailbox "Years" of account "iCloud" -- Adjust if the path is different

-- List of sub-mailboxes under 'Years', each named with a four-digit year

set yearFolders to every mailbox of yearsMailbox

-- Specify the source folder where emails are currently located

set sourceFolder to mailbox "Recovered" -- Replace "SourceFolder" with the name of your folder

repeat with eachMessage in (get messages of sourceFolder)

set messageDate to date received of eachMessage

set messageYear to year of messageDate as string

-- Check if a folder for the year exists and move the message

repeat with eachYearFolder in yearFolders

if name of eachYearFolder is messageYear then

move eachMessage to eachYearFolder

exit repeat

end if

end repeat

end repeat

end tell





Please leave comments below.