Thanks Shane.
I knew the handlers resetting correctly the original clipboard because you posted them here some weeks ago but it's not what is puzzling me.
I inserted the handlers in the script and tested : --{code}
use scripting additions use framework "Foundation" use framework "AppKit" -- for NSPasteboard
on run my germaine() end run
on germaine() set testBlank to false
tell application "System Events" -- get frontmost process set frontmostProcess to first process where it is frontmost -- this will be the script process if name of frontmostProcess is in {"Script Editor", "AppleScript Editor"} then set visible of frontmostProcess to false -- hide the script process repeat while (frontmostProcess is frontmost) -- wait until the script is hidden delay 0.2 end repeat set theApp to name of first process where it is frontmost -- get name of frontmost process (ignoring the script process) set frontmost of frontmostProcess to true -- unhide the script process else set theApp to name of frontmostProcess end if end tell set theDate to current date
set theClip to my fetchStorableClipboard() # save it to reset it on exit set leMessage to "Yvan KOENIG running Yosemite 10.10.4 in French (VALLAURIS, France) " & date string of theDate & space & time string of theDate my restoreClipboard:{leMessage} --set the clipboard to lemessage tell application "System Events" tell process theApp set frontmost to true delay 0.5 keystroke "v" using {command down} if testBlank then keystroke return & space else keystroke return & leMessage & return end if end tell end tell my restoreClipboard:theClip end germaine
on fetchStorableClipboard() set aMutableArray to current application's NSMutableArray's array() -- used to store contents -- get the pasteboard and then its pasteboard items set thePasteboard to current application's NSPasteboard's generalPasteboard() set theItems to thePasteboard's pasteboardItems() -- loop through pasteboard items repeat with i from 1 to count of theItems -- make a new pasteboard item to store existing item's stuff set newPBItem to current application's NSPasteboardItem's alloc()'s init() -- get the types of data stored on the pasteboard item set theTypes to (item i of theItems)'s types() -- for each type, get the corresponding data and store it all in the new pasteboard item repeat with j from 1 to count of theTypes set theData to ((item i of theItems)'s dataForType:(item j of theTypes))'s mutableCopy() -- mutableCopy makes deep copy if theData is not missing value then (newPBItem's setData:theData forType:(item j of theTypes)) end if end repeat -- add new pasteboard item to array (aMutableArray's addObject:newPBItem) end repeat return aMutableArray end fetchStorableClipboard
on restoreClipboard:theArray -- get pasteboard set thePasteboard to current application's NSPasteboard's generalPasteboard() -- clear it, then write new contents thePasteboard's clearContents() thePasteboard's writeObjects:theArray end restoreClipboard: --===== --{code}
When the variable testBlank is set to false, the script insert the correct signature (with août) and the wrong one (with augt). When the variable testBlank is set to true, the script insert the entry clipboard.
This remain a perfectly obscure behavior.
After exchanging directly with Shane, I had the idea to make an other attempt replacing in leMessage the character û by the pair ^u which is what we have to press to get the character.
Some days ago, I tried to use this scheme with odd result but may I was too nervous and made someting wrong.
Today I tested the code :
--{code}
use scripting additions use framework "Foundation" use framework "AppKit" -- for NSPasteboard
on run my germaine() end run
on germaine()
tell application "System Events" -- get frontmost process set frontmostProcess to first process where it is frontmost -- this will be the script process if name of frontmostProcess is in {"Script Editor", "AppleScript Editor"} then set visible of frontmostProcess to false -- hide the script process repeat while (frontmostProcess is frontmost) -- wait until the script is hidden delay 0.1 end repeat set theApp to name of first process where it is frontmost -- get name of frontmost process (ignoring the script process) set frontmost of frontmostProcess to true -- unhide the script process else set theApp to name of frontmostProcess end if end tell
set theClip to my fetchStorableClipboard() # save it to reset it on exit
set theDate to current date
set leMessage to "Yvan KOENIG running Yosemite 10.10.4 in French (VALLAURIS, France) " & date string of theDate & space & time string of theDate if month of theDate as number is 8 then set leMessage to my remplace(leMessage, "û", "^u") end if my restoreClipboard:{leMessage} --set the clipboard to lemessage
tell application "System Events" to tell process theApp set frontmost to true keystroke return & leMessage & return end tell my restoreClipboard:theClip end germaine
on fetchStorableClipboard() set aMutableArray to current application's NSMutableArray's array() -- used to store contents -- get the pasteboard and then its pasteboard items set thePasteboard to current application's NSPasteboard's generalPasteboard() set theItems to thePasteboard's pasteboardItems() -- loop through pasteboard items repeat with i from 1 to count of theItems -- make a new pasteboard item to store existing item's stuff set newPBItem to current application's NSPasteboardItem's alloc()'s init() -- get the types of data stored on the pasteboard item set theTypes to (item i of theItems)'s types() -- for each type, get the corresponding data and store it all in the new pasteboard item repeat with j from 1 to count of theTypes set theData to ((item i of theItems)'s dataForType:(item j of theTypes))'s mutableCopy() -- mutableCopy makes deep copy if theData is not missing value then (newPBItem's setData:theData forType:(item j of theTypes)) end if end repeat -- add new pasteboard item to array (aMutableArray's addObject:newPBItem) end repeat return aMutableArray end fetchStorableClipboard
on restoreClipboard:theArray -- get pasteboard set thePasteboard to current application's NSPasteboard's generalPasteboard() -- clear it, then write new contents thePasteboard's clearContents() thePasteboard's writeObjects:theArray end restoreClipboard:
#===== (* replaces every occurences of d1 by d2 in the text t *) on remplace(t, d1, d2) local oTIDs, l set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1} set l to text items of t set AppleScript's text item delimiters to d2 set t to l as text set AppleScript's text item delimiters to oTIDs return t end remplace
#=====
And, bingo, I get the wanted behavior.
The pig headed guy is always puzzled by the odd behavior but at least, he has a working script ;-)
Yvan KOENIG running Yosemite 10.10.4 in French (VALLAURIS, France) lundi 3 août 2015 14:40:26
|