A journaling stop watch for mundane tasks and time slips

Hello.

I have written a script to configure the info.plist file of an app, in order to configure it as a service, I have added both! properties for making it into a background only app, and a user agent. This for the hope that at least one of them, sees to, that launchd skips the part of the launch process that loads UI.

Other than that: You can only run this script once per propertylist file. And you should really duplicate the info.plist file first, so you have a backup, should something go wrong. (just duplicate it in Finder. Of course have read Mark Hunte’s post about this, -which I refer to in post nr 1.

Install

This script must be installed into the script menu, and you should change the name to “Script Editor” if on Yosemite.

Usage

See to that the application that you want to configure is the frontmost, and then run this script, after you have configured it with the values you want to use.

:slight_smile:

-- Copyright 2015 ©  McUsr
-- Copyright 2015 ©  McUsr
# You may not post, or print  this somewhere else, but refer to the link where you found it.
property scriptTitle : "Configure Applet as Service"
-- This script is, as it stands just able to be run on once, it doesn't change any already 
-- existing values, it probably will ruin the whole plist file, so it is a good thing to have
-- made a copy up front.

property serviceName : "Launch Mail"
-- Above is the name of the service as it should turn up in any services menu.
property serviceMessage : "runAService"
-- Above is the name of the handler in your applet that is the services run-handler.
property servicePortName : "LaunchMail"
-- above is the name of the service portname the service should be called with.
-- I think it wise to call it the same as the Application 
property dryRun : false


tell application "AppleScript Editor"
	if serviceName = "Launch Console" and servicePortName = "LaunchConsole" then
		display dialog "It appears that you haven't edited the original values of the script, are you sure you want to enter those values into an info.plist of an applet you intend to use as a service?" with title my scriptTitle with icon 2
	end if
	
	try
		-- Check to see if the front document has been saved as a bundle application
		try
			set the doc_properties to the (properties of the front document)
			set the script_path to the path of doc_properties
			set appletName to name of doc_properties
			set the plist_path to script_path & "/Contents/Info.plist"
			set the plist_file to plist_path as POSIX file as alias
		on error
			error "The front script document has not be saved as a Script bundle or as an Application bundle."
		end try
		
		
		-- Query the user
		
		set the dialog_message to "This scripts sets necessary for an application based on the cocoa-applet to be configured as a Service.
You have service name to : -> " & my serviceName & " <-,
     the service message to: -> " & my serviceMessage & " <-,
 the service port name to : -> " & my servicePortName & " <-" & return & "
Are those the properterties you want to use on: -> " & appletName & " <- , for it to become a Service?"
		set {button returned:btn} to (display dialog dialog_message with title my scriptTitle buttons {"No", "Yes"} cancel button 1 default button 2 with icon 2)
		
		if btn = "Yes" then
			tell application "System Events"
				-- create an empty property list dictionary item
				set the parent_dictionary to make new property list item with properties {kind:record}
				-- create new property list file using the empty dictionary list item as contents
				if my dryRun then
					set the plistfile_path to "~/Desktop/ServiceExample.plist"
					
					set this_plistfile to ¬
						make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
				else
					set this_plistfile to property list file plist_path
				end if
				if (exists property list item "NSServices" of contents of this_plistfile) then
					display alert my scriptTitle message "The property list already contains an NSServices item, I quit" buttons {"Cancel"} cancel button 1
				end if
				make new property list item at end of property list items of contents of this_plistfile ¬
					with properties {kind:boolean, name:"LSUIElement", value:true}
				
				make new property list item at end of property list items of contents of this_plistfile ¬
					with properties {kind:string, name:"LSBackgroundOnly", value:"1"}
				
				
				set servicesArray to make new property list item at end of property list items of contents of this_plistfile ¬
					with properties {kind:list, name:"NSServices"}
				set servicesDict to make new property list item at end of property list items of contents of servicesArray ¬
					with properties {kind:record}
				set menuItemDict to make new property list item at end of property list items of contents of servicesDict ¬
					with properties {kind:record, name:"NSMenuItem"}
				make new property list item at end of property list items of contents of menuItemDict ¬
					with properties {kind:string, value:my serviceName, name:"default"}
				
				-- Inserting into Services dict
				make new property list item at end of property list items of contents of servicesDict ¬
					with properties {kind:string, value:my serviceMessage, name:"NSMessage"}
				make new property list item at end of property list items of contents of servicesDict ¬
					with properties {kind:string, value:my servicePortName, name:"NSPortName"}
				-- creating the empty array:
				make new property list item at end of property list items of contents of servicesDict ¬
					with properties {kind:list, name:"NSSendTypes"}
				
			end tell
		end if
		
		display notification appletName & " configured to be a Service." with title scriptTitle
		
	on error error_message number error_number
		if the error_number is not -128 then
			display dialog error_message with title scriptTitle buttons {"Cancel"} default button 1 with icon 2
		end if
	end try
end tell