Category: AppleScript

  • Smart Finder Tabs Launcher for macOS AppleScript

    This AppleScript streamlines your Finder workspace by checking for an existing Finder window and reusing it to open specified folders as individual tabs, thereby avoiding duplicate windows or tabs. Customize the folders by editing the definitions at the top of the script.

    The folder paths are defined near the beginning of the script. For example, you can customize the following lines to change which folders open:

    set docsFolder to (path to documents folder)
    set desktopFolder to (path to desktop folder)
    set downloadsFolder to (path to downloads folder)


    Simply modify these variables to point to your desired folder paths.

    Click to view script…
    -- Title: Finder Tabs Organizer
    -- Description:
    -- This AppleScript opens designated folders as tabs in Finder
    -- Customize the folders to open by modifying the folder definitions below.
    
    -- Handler to open a new tab for a given folder
    on openNewTabWithFolder(theFolder)
        tell application "System Events"
            keystroke "t" using {command down} -- simulate Command+T to open a new tab
        end tell
        delay 0.5
        tell application "Finder"
            set target of front Finder window to theFolder
        end tell
    end openNewTabWithFolder
    
    -- Define the folders to open
    set docsFolder to (path to documents folder)
    set desktopFolder to (path to desktop folder)
    set downloadsFolder to (path to downloads folder)
    
    tell application "Finder"
        activate
        if (count of Finder windows) > 0 then
            -- Use the existing Finder window and set its target to Documents
            set target of front Finder window to docsFolder
        else
            -- No window exists, so open a new window for Documents
            open docsFolder
        end if
    end tell
    
    delay 0.5
    
    -- Open additional tabs for the other folders.
    openNewTabWithFolder(desktopFolder)
    openNewTabWithFolder(downloadsFolder)

  • Scheduled URL Launcher AppleScript

    This AppleScript prompts the user to input multiple URLs and offers a choice to open them immediately or after a countdown timer. For immediate launches, it confirms the URLs before opening, while for countdown mode it notifies the user that the countdown has started and then provides a final confirmation after the URLs have been opened.

    Click to view script…
    -- Create a large default text field by including multiple line breaks
    set multiLineDefault to return & return & return & return
    
    -- Prompt the user for a list of URLs (one URL per line)
    set urlInput to text returned of (display dialog "Enter list of URLs (one per line):" default answer multiLineDefault)
    
    -- Split the input into a list of lines
    set urlList to paragraphs of urlInput
    
    -- Ask the user if they want to open the URLs now or after a countdown
    set schedulingOption to button returned of (display dialog "Choose when to open the URLs:" buttons {"Now", "Countdown"} default button "Now")
    
    if schedulingOption is "Now" then
    	-- Build a confirmation message listing the URLs to be opened immediately
    	set confirmDialogText to "The following URLs will be opened now:" & return & return
    	repeat with aURL in urlList
    		set trimmedURL to trimWhitespace(aURL)
    		if trimmedURL is not "" then
    			-- Prepend protocol if missing
    			if (trimmedURL does not start with "http://") and (trimmedURL does not start with "https://") then
    				set trimmedURL to "https://" & trimmedURL
    			end if
    			set confirmDialogText to confirmDialogText & trimmedURL & return
    		end if
    	end repeat
    	
    	-- Show confirmation dialog before launching
    	set confirmResponse to button returned of (display dialog confirmDialogText buttons {"Cancel", "Open Now"} default button "Open Now")
    	if confirmResponse is "Cancel" then return
    	
    	-- Open URLs immediately
    	repeat with aURL in urlList
    		set trimmedURL to trimWhitespace(aURL)
    		if trimmedURL is not "" then
    			if (trimmedURL does not start with "http://") and (trimmedURL does not start with "https://") then
    				set trimmedURL to "https://" & trimmedURL
    			end if
    			open location trimmedURL
    		end if
    	end repeat
    	
    else if schedulingOption is "Countdown" then
    	-- Prompt for countdown seconds
    	set countdownInput to text returned of (display dialog "Enter countdown time in seconds:" default answer "10")
    	try
    		set countdownSeconds to countdownInput as integer
    	on error
    		display dialog "Invalid number. Exiting." buttons {"OK"} default button "OK"
    		return
    	end try
    	
    	-- Notify the user that the countdown is starting
    	display dialog "Countdown started for " & countdownSeconds & " seconds. The URLs will open automatically afterwards." buttons {"OK"} default button "OK"
    	
    	-- Delay for the specified number of seconds
    	delay countdownSeconds
    	
    	-- Open URLs after the delay
    	repeat with aURL in urlList
    		set trimmedURL to trimWhitespace(aURL)
    		if trimmedURL is not "" then
    			if (trimmedURL does not start with "http://") and (trimmedURL does not start with "https://") then
    				set trimmedURL to "https://" & trimmedURL
    			end if
    			open location trimmedURL
    		end if
    	end repeat
    	
    	-- Build a final confirmation message listing the URLs that were opened
    	set finalConfirmDialogText to "The following URLs have been opened:" & return & return
    	repeat with aURL in urlList
    		set trimmedURL to trimWhitespace(aURL)
    		if trimmedURL is not "" then
    			if (trimmedURL does not start with "http://") and (trimmedURL does not start with "https://") then
    				set trimmedURL to "https://" & trimmedURL
    			end if
    			set finalConfirmDialogText to finalConfirmDialogText & trimmedURL & return
    		end if
    	end repeat
    	
    	-- Show the final confirmation dialog
    	display dialog finalConfirmDialogText buttons {"OK"} default button "OK"
    end if
    
    --------------------------------------------------------------------------------
    -- Helper Handler: trimWhitespace
    -- Removes leading and trailing whitespace from a string
    --------------------------------------------------------------------------------
    on trimWhitespace(theText)
    	set AppleScript's text item delimiters to {" ", tab, return, linefeed}
    	set textItems to text items of theText
    	set AppleScript's text item delimiters to space
    	set trimmed to textItems as text
    	set AppleScript's text item delimiters to ""
    	return trimmed
    end trimWhitespace
  • Automated Screen Capture AppleScript Tool

    This AppleScript lets you capture screenshots either on demand or at scheduled intervals, automatically saving each capture in a designated folder with a unique, timestamped filename. Notifications alert you whenever a screenshot is taken, ensuring you always know when your screen has been captured.

    Warning: This script requires specific macOS permissions to function correctly. In particular, macOS Catalina and later require that you enable Screen Recording permissions for the app running the script (e.g., Script Editor or Terminal), and you may also need to grant Automation or Full Disk Access in Security & Privacy settings. Without these permissions, the screenshot functionality and notifications might not work as expected.

    Click to view script…
    -- Automated Screen Capture Script
    
    -- Ensure the "Screenshots" folder exists on the Desktop
    do shell script "mkdir -p ~/Desktop/Screenshots"
    
    -- Ask the user to choose a capture mode
    display dialog "Choose Screenshot Mode:" buttons {"On Demand", "Scheduled"} default button "On Demand"
    set modeChoice to button returned of result
    
    if modeChoice is "On Demand" then
        my takeScreenshot()
    else if modeChoice is "Scheduled" then
        display dialog "Enter interval between screenshots (in seconds):" default answer "60"
        set intervalSeconds to (text returned of result) as integer
        
        display dialog "Enter number of screenshots to capture:" default answer "10"
        set numberShots to (text returned of result) as integer
        
        repeat with i from 1 to numberShots
            my takeScreenshot()
            delay intervalSeconds
        end repeat
    end if
    
    -- Handler to take a screenshot and save it with a timestamped filename
    on takeScreenshot()
        -- Create a timestamp
        set timeStamp to do shell script "date +%Y%m%d-%H%M%S"
        set fileName to "Screenshot-" & timeStamp & ".png"
        set filePath to "~/Desktop/Screenshots/" & fileName
        -- Capture the screenshot silently (-x suppresses the shutter sound)
        do shell script "screencapture -x " & filePath
        display notification "Captured: " & fileName with title "Screen Capture"
    end takeScreenshot
  • Flexible Volume Controller AppleScript

    This AppleScript lets you adjust your Mac’s system volume either by selecting from a list of predefined levels (Mute, 0, 25, 50, 75, or 100) or by entering a custom value between 0 and 100, with a notification confirming the change.

    Click to view script…
    -- Volume Controller Script
    
    -- First, choose the control method.
    display dialog "Choose Volume Control Method:" buttons {"Predefined Levels", "Custom Level"} default button "Predefined Levels"
    set methodChoice to button returned of result
    
    if methodChoice is "Predefined Levels" then
        -- Use a list to display multiple predefined options.
        set volumeOptions to {"Mute", "0", "25", "50", "75", "100"}
        set volChoiceList to choose from list volumeOptions with prompt "Select Volume Setting:" default items {"50"}
        
        if volChoiceList is false then
            display notification "No selection made." with title "Volume Controller"
            return
        end if
        
        set volChoice to item 1 of volChoiceList
        if volChoice is "Mute" then
            set volume with output muted
            display notification "Volume muted" with title "Volume Controller"
        else
            set volValue to volChoice as integer
            set volume output volume volValue
            display notification "Volume set to " & volValue & "%" with title "Volume Controller"
        end if
        
    else if methodChoice is "Custom Level" then
        display dialog "Enter desired volume level (0-100):" default answer "50"
        set volInput to text returned of result
        set volLevel to volInput as integer
        
        if volLevel < 0 then
            set volLevel to 0
        else if volLevel > 100 then
            set volLevel to 100
        end if
        
        set volume output volume volLevel
        display notification "Volume set to " & volLevel & "%" with title "Volume Controller"
    end if
  • Light and Dark Mode Toggler AppleScript

    This AppleScript toggles your macOS display between dark and light modes, letting you quickly adapt your favorite setting.

    Click to view script…
    tell application "System Events"
        tell appearance preferences
            set dark mode to not dark mode
        end tell
    end tell

  • Dynamic Folder Generator on macOS with AppleScript

    This AppleScript streamlines folder creation by prompting users for the number of folders, a base name, and a destination location. It then automatically creates sequentially numbered folders in the chosen directory, simplifying organization and file management on macOS.

    Click to view script…
    -- Ask the user how many folders they want to create
    set folderCountString to text returned of (display dialog "How many folders do you want to create?" default answer "1")
    try
        set folderCount to folderCountString as integer
    on error
        display dialog "Please enter a valid number."
        return
    end try
    
    -- Ask for a base name for the folders
    set baseName to text returned of (display dialog "Enter a base name for your folders:" default answer "Folder")
    
    -- Let the user choose the target location (e.g., Desktop, Documents, Downloads, etc.)
    set targetFolder to choose folder with prompt "Choose the folder where the new folders will be created:"
    
    -- Create the folders
    tell application "Finder"
        repeat with i from 1 to folderCount
            set folderName to baseName & " " & i
            try
                make new folder at targetFolder with properties {name:folderName}
            on error errMsg number errNum
                display dialog "Error creating folder \"" & folderName & "\": " & errMsg
            end try
        end repeat
    end tell
    
    display dialog "Successfully created " & folderCount & " folder(s) in " & (targetFolder as text)
  • Simple Scheduled Website Opener AppleScript

    This AppleScript prompts you to enter a delay in seconds and a website URL, then opens the specified website in your default browser after the delay. A confirmation dialog informs you that the site will launch after you close it, making it a straightforward scheduling tool for web access.

    Click to view script…
    -- Simple Scheduled Website Opener
    -- Prompts the user for a delay (in seconds) and a website URL, then opens the URL after the delay.
    
    -- Ask the user for the delay in seconds
    set delayResponse to display dialog "Enter the number of seconds to wait before opening the website:" default answer "10"
    set delaySeconds to (text returned of delayResponse) as number
    
    -- Ask the user for the website URL
    set urlResponse to display dialog "Enter the website URL to open:" default answer "https://"
    set websiteURL to text returned of urlResponse
    
    -- Confirmation dialog informing the website will open after the dialog is closed
    display dialog "The website " & websiteURL & " will open in " & delaySeconds & " seconds after you close this dialog." buttons {"Cancel", "OK"} default button "OK"
    if button returned of result is "Cancel" then return
    
    -- Wait for the specified delay
    delay delaySeconds
    
    -- Open the website in the default browser
    do shell script "open " & quoted form of websiteURL

  • Rickroll Launcher AppleScript

    This AppleScript command uses a shell command to open a popular YouTube video in your default browser, effectively “Rickrolling” the user. It’s a playful example of how to execute shell scripts from within AppleScript.

    Click to view script…
    do shell script "open https://www.youtube.com/watch?v=dQw4w9WgXcQ"
  • Simulating a macOS Crash Using AppleScript

    This AppleScript launches the macOS screensaver to create the appearance of an unresponsive system, then plays a voice message saying, “Uh oh. Something went wrong.” The combination can be used for testing reactions or demonstrating AppleScript’s ability to control system functions.

    Click to view script…
    do shell script "open /System/Library/CoreServices/ScreenSaverEngine.app"
    delay 2
    say "Uh oh. Something went wrong."