Issue With Quitting App

Sometimes the simplest things seem the hardest to figure out. I have a clipboard app (Copy ‘Em) that works great, but it syncs everything that’s on the clipboard via iCloud. In the app you can have the iCloud sync items cleared if you set the app (in Preferences) to “Auto Delete Unstarred Items When App Quits”. But that alone does not clear the iCloud items. Once you quit the app, you need to restart it to actually finish the clearing of the iCloud items. To do that I’ve put together a couple of scripts (see below). The issue is that at times when running either script, I get a popup window that states “Copy ‘Em in not running”, although I can see its menu bar icon, so I know it’s running. To try to solve the issue of if it’s running or not, I’ve done searches here and the net in general, but not come up with much. I have seen many mentions of scripts that check to see if an app is running, or others that check and set something to “true”. One of the issues that I keep having is that the app name “Copy ‘Em” contains a ‘ just before the “Em” and most scripts to check if the app is running stumble over that ‘ in the name.

Any suggestions and/or help would be appreciated.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

delay 3
tell application "Copy 'Em" to quit
delay 3
tell application "Copy 'Em" to activate
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

delay 3
tell application "Copy 'Em"
	quit
end tell

delay 3
tell application "Copy 'Em"
	activate
end tell

Found something here on MacScripter: possible to check if an application is running with AppleScript?

Which may work, but it seems a bit too involved (see script below). And, without more testing I don’t know if it will take care of the “Copy 'Em is not running” issue.

delay 3
tell application "Copy 'Em" to quit
delay 3

tell application "System Events"
	set AppleScript's text item delimiters to {","}
	set theprocess to name of every process as list
end tell

if theprocess does not contain "Copy 'Em" then
	tell application "Copy 'Em" to activate
end if

Actually, the above really won’t work as it starts off with the same “quit” sequence as my original scripts. What I’m hoping will work is turning the second part of the above script into both a quit and a restart.

tell application "System Events"
	set AppleScript's text item delimiters to {","}
	set theprocess to name of every process as list
end tell

if theprocess contains "Copy 'Em" then
	tell application "Copy 'Em" to quit
end if

delay 3

tell application "System Events"
	set AppleScript's text item delimiters to {","}
	set theprocess to name of every process as list
end tell

if theprocess does not contain "Copy 'Em" then
	tell application "Copy 'Em" to activate
end if

Try something a little bit different like this…

if application "Copy 'Em" is running then tell application "Copy 'Em" to quit

tell application "System Events"
	if process "Copy 'Em" exists then
		repeat while process "Copy 'Em" exists
			process "Copy 'Em" exists
			delay 0.1
		end repeat
	end if
end tell

delay 1
tell application "Copy 'Em" to activate

How about activating before quitting?

tell application "Copy 'Em"
	activate
	quit
	delay 3
	activate
end tell

Thank you both “wch1zpink” and “gluebyte” for the suggestions. But the “Copy 'Em is not running” issue only comes up maybe a couple of times each week, so it’ll take a while for me to find a solution that works consistently. But at least I have additional options to choose from, so thanks again.

Your middle portion of your script could be cleaned up like so…

if application "Copy 'Em" is running then tell application "Copy 'Em" to quit

tell application "System Events"
	repeat while process "Copy 'Em" exists
		delay 0.2
	end repeat
end tell

delay 1
tell application "Copy 'Em" to activate
1 Like

I’d like to strongly advise against using special characters like single quotes in file names.
The chance to run into serious problems is pretty high.

The application ID is actually “Copy-em-Paste”, would you suggest using that instead of “Copy 'Em”?

Yes, I do, hyphens are much better than single quotes.

Finally gave up. The issue is not with any of the scripts, the issue is with the app itself. For some reason it does not register (if that’s the right word) with the MacOS system that it’s active, which was confirmed by the developer. So, I’ve uninstalled it and back to using another clipboard manager, Clipy.

Interesting. A killall shell script won’t make it quit either? Or did you specifically need to quit it in a “clean” way (killall would force-quit the app)? But I guess you don’t care anymore anyway.