You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to make an applescript into a service?

I don't see any way to create a service from within Big Sur's Script Editor. Am I missing something?


TIA,

David

Posted on Jan 5, 2021 7:48 AM

Reply
14 replies

Jan 5, 2021 2:44 PM in response to drschwartz

Ok. I have a working Automator application using the GUI scripting code that I posted in your previous post. I added a Get Specified Finder Items action to use the name of a text file containing the list of cookies you want to remove from Safari. This way, you only have to edit this cookies.txt file and not the hard-coded list within the AppleScript code.


Because of the increased security measures in recent operating systems, there are some System Preferences : Security & Privacy additions one must make to avoid annoying restriction dialogs apppearing, and the ability to actually send keystrokes from an Automator application.


First, launch Automator from your Applications folder, Choose new Document, and select Application, then click Choose. Next, you only need to drag and drop the following actions from the libary, and in order onto the larger workflow window:


  1. Files & Folders : Get Specified Finder Items
    1. Add the name of the text file that you will be using to contain the list of cookies to remove, one to a line, unquoted.
  2. Utilities : Run AppleScript


The Run AppleScript action will default to a boilerplate on run handler. Select and remove everything in this Run AppleScript action. Then replace with the following code:


use scripting additions

on run {input, parameters}
	
	# input is the name of the file passed in from Get Specified Finder Items
	# read the contents of the input text file and automatically generate a list of cookies to remove
	set deCookie to (read file (input as text) as text using delimiter linefeed) as list
	
	# Safari must be running before this script can work properly
	tell application "Safari" to if it is not running then activate
	
	tell application "System Events"
		tell application process "Safari"
			set frontmost to true
			keystroke "," using command down
			delay 1
			tell window 1
				click button "Privacy" of toolbar 1 of it
				try
					repeat with d in deCookie
						click button "Manage Website Data…" of group 1 of group 1
						-- give cookies time to load in their scrollable window
						delay 2
						select -- focuses on the search window
						try
							keystroke d
							delay 2
							select row 1 of table 1 of scroll area 1 of sheet 1
							delay 1
							click button "Remove" of sheet 1
						end try
					end repeat
					click button "Done" of sheet 1
				on error errmsg number errnbr
					click button "Done"
					keystroke "w" using command down
					display alert "No cookies to remove" giving up after 5
					return
				end try
			end tell
			keystroke "w" using command down
			display alert "Selected cookies removed" giving up after 5
		end tell
	end tell
	# because if you don't do this, the next time you open Preferences it will be hung
	tell application "Safari" to if it is running then quit
	return
end run


Click the hammer icon within the Run AppleScript action to compile the AppleScript code, and then you can save the Automator Application to your Desktop. I gave mine the name Remove_Cookies, and I would resist using white-space in the application name.


Now, you have some security work to do before you can run it. Open System Preferences : Security & Privacy with your administrator password. Do the following:

  1. Accessibility
    1. Add your just saved Automator Application name here, and ensure it is selected.
  2. Full Disk Access
    1. Add your Automator Application name here too
    2. Add Automator too
  3. Files and Folders
    1. Add your Automator Application name here.
    2. Automator should have already been added


Now, once you have built your text file of cookies to remove, you can double-click the saved Automator application on your Desktop and the familiar Safari Privacy panel antics will appear for each cookie that you have added to the cookie.txt file. If you need to edit your saved application, just launch Automator, and select open Recent.


Jan 5, 2021 8:54 AM in response to drschwartz

It has script editor but there's no save to services feature in Mavericks that I can see. I just save my Applescript, use Automator to turn it into a workflow, then I think I moved that manually to my Services folder. I don't remember exactly since I did this once about 3 years ago to turn some of my F keys into tray open and close keys for an external drive on an iMac. The eject on my keyboard didn't work with external drives.

Jan 5, 2021 10:46 AM in response to drschwartz

One launches Automator, and in Big Sur, chooses Quick Action (aka Service) to create a service workflow. Then from the Utilities library, select Run AppleScript and drag it onto the right-hand workflow window. You would insert your AppleScript within the on run clause, and what every you selected for the service to action would be in the input variable. You then save the Quick Action with a short, but meaningful name (without any extension) and it is written into your /Users/yourname/Library/Services folder.


Automator solutions are workflows in that the output of one action flows into the input of another like a waterfall. It is best to get the AppleScript code working in Script Editor where you can see when things go wrong, as there is no such debugging capability in Automator. One syntax error and it just quits with no helpful message why.


If you can explain what you are attempting to construct for a specific outcome, you might get more guidance on how to proceed.



Jan 5, 2021 1:31 PM in response to drschwartz

Sorry, kind of fried and didn't pay attention to your name. I probably would not create a service (Quick Action) for what you are trying to do, but rather an Automator application that would simply run, remove the selected cookies from Safari, and then quit. Application sits on your Desktop, where you just double-click it to run the cookie removal functionality.


Let's see if I can whip up something here for you.

Jan 7, 2021 10:27 AM in response to drschwartz

What's going on with that Automator application progress? I would reboot the Mac, and start over following my instructions from the beginning (e.g. launch Automator and choose Application, etc.


I have been creating and saving Automator applications for years on Macs with no developer tools installed, and those with Xcode and/or the command-line developer tools installed, and have never once seen the Automator error dialog that you posted.

How to make an applescript into a service?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.