[pass] [PATCH] applescript: restore last active window after password has been copied to clipboard

Steffen Vogel post at steffenvogel.de
Fri Jul 24 10:28:50 CEST 2015


This change improves the UX by not distrubing the workflow.
After the password was copied to the clipboard the previously active app
will be activated again.
---
 contrib/pass.applescript | 70 +++++++++++++++++++++++++++---------------------
 1 file changed, 40 insertions(+), 30 deletions(-)

diff --git a/contrib/pass.applescript b/contrib/pass.applescript
index 4720fff..c71fd93 100644
--- a/contrib/pass.applescript
+++ b/contrib/pass.applescript
@@ -24,6 +24,9 @@
 --   5. Go to 'System settings' -> 'Notifications' -> choose 'Notifications Scripting' 
 --       -> and switch from 'Banners' to 'Alerts'
 --
+--   6. Go to 'System settings' -> 'Security' -> 'Privacy' -> allow helper access for the
+--       'Notifications Scripting' application as well as for the 'Automator' app.
+--
 ---------------------------------------------------------------------------------------------
 
 -- Configuration
@@ -43,50 +46,57 @@ else -- if (lang = "en")
 	set nClear to "Forget"
 end if
 
+-- Save active window
+tell application "System Events"
+	set activeProc to first application process whose frontmost is true
+end tell
+
 try
 	set entity to the text returned of (display dialog nPrompt default answer defPass buttons {"OK"} with title nTitle default button 1)
 	set pw to do shell script "export PATH=" & shellPath & "; pass " & entity
 	
 	set the clipboard to pw
-	
-	-- Wait until clipboard changed then close notification
-	repeat with secsLeft from 0 to clearAfter
-		if pw is equal to (the clipboard) then
-			tell application "Notifications Scripting"
-				set event handlers script path to (path to me)
-				display notification nTitle id "pass" message "Password copied to clipboard (" & (clearAfter - secsLeft) & " secs left)" action button nClear with has action button
-			end tell
-			delay 1
-		else
-			exit repeat
-		end if
-	end repeat
 on error errMsg
 	display dialog errMsg with title nTitle with icon stop
 end try
 
+-- Restore last active App
+tell application "System Events"
+	set the frontmost of activeProc to true
+end tell
+
+-- Wait until clipboard changed then close notification
+repeat with secsLeft from 0 to clearAfter
+	if pw is equal to (the clipboard) then
+		tell application "Notifications Scripting"
+			set event handlers script path to (path to me)
+			display notification nTitle id "pass" message "Password copied to clipboard (" & (clearAfter - secsLeft) & " secs left)" action button nClear with has action button
+		end tell
+		delay 1
+	else
+		exit repeat
+	end if
+end repeat
+
 -- Clear clipboard
 set the clipboard to ""
-closeNotifications()
 
--- Handle click to notification:
+-- Close all pending Notifications
+tell application "System Events"
+	tell process "NotificationCenter"
+		set theWindows to every window
+		repeat with i from 1 to number of items in theWindows
+			set this_item to item i of theWindows
+			try
+				click button 1 of this_item
+			end try
+		end repeat
+	end tell
+end tell
+
+-- Handle notification action: forget password
 using terms from application "Notifications Scripting"
 	on notification activated
 		set the clipboard to ""
 	end notification activated
 end using terms from
-
--- Close all Notifications
-on closeNotifications()
-	tell application "System Events"
-		tell process "NotificationCenter"
-			set theWindows to every window
-			repeat with i from 1 to number of items in theWindows
-				set this_item to item i of theWindows
-				try
-					click button 1 of this_item
-				end try
-			end repeat
-		end tell
-	end tell
-end closeNotifications
-- 
2.3.0



More information about the Password-Store mailing list