Dude Engineering

View Original

Getting Stuff Done With AppleScript

We all have them – those mind-numbing, click-heavy tasks on our Macs that eat away at our time and productivity. The tasks that make us think, "There has to be a better way!" Turns out, there is. Enter AppleScript: a hidden gem tucked away in your Mac that lets you automate those chores and streamline your workflow.

Let's create some AppleScript examples that can enhance overall productivity on a Mac. These examples will cover a range of common tasks, demonstrating how to automate and streamline your workflow.

1. Batch Rename Files in Finder

Suppose you often need to rename files in a particular format. This script will rename selected files in Finder, adding a specified prefix.

tell application "Finder"
    set selectedFiles to selection
    set filePrefix to "Project_"

    repeat with aFile in selectedFiles
        set fileName to name of aFile
        set name of aFile to filePrefix & fileName
    end repeat
end tell

2. Open a Set of Websites in Safari

If you start your day by visiting a set of websites, this script will open them all in Safari in new tabs.

tell application "Safari"
    set websiteList to {"https://www.google.com", "https://www.bbc.com", "https://www.github.com"}

    make new document
    repeat with aSite in websiteList
        tell window 1
            make new tab with properties {URL:aSite}
        end tell
    end repeat
end tell

3. Automate Email Responses in Mail

For responding to emails with a standard message, this script finds unread emails in Mail and marks them as read and replies with a predefined message.

tell application "Mail"
    set unreadMessages to (every message of inbox whose read status is false)
    set responseText to "Thank you for your email. I will get back to you soon."

    repeat with aMessage in unreadMessages
        set read status of aMessage to true
        tell aMessage
            make new outgoing message with properties {visible:true, subject:"Re: " & subject of aMessage, content:responseText & return & return & content of aMessage, <sender:"your@email.com>"}
            send
        end tell
    end repeat
end tell

4. Schedule a Daily Reminder Notification

If you need a daily reminder at a specific time, this script can be used with macOS's Calendar app to create a notification.

tell application "Calendar"
    tell calendar "Work"
        set newEvent to make new event at end with properties {description:"Daily reminder to check emails", summary:"Check Emails", start date:(current date), end date:(current date + 1 * hours)}
        make new display alarm at newEvent with properties {trigger interval:0}
    end tell
end tell

5. Organize Downloads Folder

To automatically move files from the Downloads folder to specific locations based on file type:

tell application "Finder"
    set downloadsFolder to path to downloads folder
    set pdfFolder to (path to desktop folder as string) & "PDFs:"
    set imageFolder to (path to desktop folder as string) & "Images:"

    repeat with aFile in (get files of downloadsFolder)
        if name extension of aFile is "pdf" then
            move aFile to folder pdfFolder
        else if (name extension of aFile is "jpg") or (name extension of aFile is "png") then
            move aFile to folder imageFolder
        end if
    end repeat
end tell

Notes for Usage

  • Customization: Customize these scripts according to your specific needs and paths.
  • Triggering Scripts: You can run these scripts from the Script Editor, assign them to keyboard shortcuts, or use automation tools like Automator or Keyboard Maestro.
  • Testing: Always test scripts in a controlled environment before implementing them in your workflow.

6. Refresh All Tabs in browser

Refresh all the tabs of the current active browser window:

try
    tell application "System Events"
        set frontAppName to name of first application process whose frontmost is true
    end tell
    display dialog "The frontAppName is: " & frontAppName
on error errMsg
    display dialog "The errMsg is: " & errMsg
end try

-- Handler to reload tabs
on reloadTabsInBrowser(browserName)
    try
        if browserName is "Safari" then
            tell application "Safari"
                repeat with theWindow in every window
                    repeat with theTab in every tab of theWindow
                        tell theTab to reload
                    end repeat
                end repeat
            end tell
        else if browserName is "Brave Browser" then
            -- Add the specific commands for Brave Browser here
        else if browserName is "Arc" then
            -- Add the specific commands for Arc here
        else
            return "Unsupported browser: " & browserName
        end if
        return "Refreshed all tabs in " & browserName
    on error errMsg
        return "Error in reloading tabs: " & errMsg
    end try
end reloadTabsInBrowser

-- Calling the handler
set outcome to reloadTabsInBrowser(frontAppName)
display dialog outcome

These examples demonstrate the versatility of AppleScript in automating various tasks, ultimately saving time and increasing productivity. Remember, the key to getting the most out of AppleScript is experimentation and practice. Don't be afraid to tweak the scripts, explore new commands, and tailor the scripts to suit your specific needs. With AppleScript, you have the power to transform your Mac into a more powerful ally in your quest for productivity and efficiency. So go ahead, start scripting, and discover the myriad ways AppleScript can help you get stuff done.