Hey Omar,
That script will do NOTHING with so-called “hidden” windows. It ONLY makes HIDDEN applications visible.
NOTE:
Any window that has been minimized or turned into a full-screen window is at the mercy of the system and may or may NOT be available to a normal scripted solution. You have to test to be sure of these things.
Because of Apple's ham-handed handling of Spaces I rarely use them — particularly since they can interfere with scripting.
Rule-of-the-thumb — never, ever use the Clipboard when a more direct means of getting the selection is available.
Here's now I'd handle your task.
Since you're dealing with plain-text it's relatively simple to gather all your required data using some AppleScript, some JavaScript – then massage the data a little and write it to a file with the Satimage.osax.
This method should be lightning-fast and very reliable.
NOTE — You the user need to set the variable logFilePath to the correct POSIX Path. If the target file does not exist the script will fail.
------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/05/06 02:00
# dMod: 2017/05/07 08:23
# Appl: Google Chrome & Safari
# Task: Write Selected Text to a Log File with an Info-Header.
# Libs: None
# Osax: Satimage.osax --> http://tinyurl.com/satimage-osaxen
# Tags: @Applescript, @Script, @System_Events, @Write, @Selected, @Text, @Log, @File, @Info-Header, @Header
# Vers: 1.02
------------------------------------------------------------------------------
# Note: This script REQUIRES the Satimage.osax AppleScript Extension to be INSTALLED!!!!
------------------------------------------------------------------------------
try
set logFilePath to "~/Documents-LC3/_loggar/my-touched-pages-IN.txt" -- POSIX-Path or Tilde-POSIX-Path
tell application "System Events" to set logFilePath to POSIX path of disk item logFilePath
set logDate to current date
set logDate to strftime logDate into "logged tpp %m/%d/%Y %H:%M =="
set pathToFrontAppHFS to path to frontmost application as text
if pathToFrontAppHFS ends with "Google Chrome.app:" then
set theApp to "Chrome"
set documentTitle to doJavaScriptInChrome("document.title")
set documentURL to doJavaScriptInChrome("document.URL")
set selectedText to doJavaScriptInChrome("window.getSelection()+'';")
if selectedText = "" then error "No text was selected in Chrome!"
set selectedText to cng("\\A\\s+|\\s+\\Z", "", selectedText) of me
else if pathToFrontAppHFS ends with "Safari.app:" then
set theApp to "Safari"
set documentTitle to doJavaScriptInSafari("document.title")
set documentURL to doJavaScriptInSafari("document.URL")
set selectedText to doJavaScriptInSafari("window.getSelection()+'';")
if selectedText = "" then error "No text was selected in Safari!"
set selectedText to cng("\\A\\s+|\\s+\\Z", "", selectedText) of me
else
error "Google Chrome or Safari must be the frontmost app!"
end if
set logText to join {logDate, "URL: " & documentURL, documentTitle, "", selectedText, "", ""} using linefeed
set outputText to logText & (readtext logFilePath)
writetext outputText to logFilePath
display notification "Log text written!" with title theApp subtitle "" sound name "Tink"
on error e number n
stdErr(e, n, true, true) of me
end try
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cng(_find, _replace, _data)
change _find into _replace in _data with regexp without case sensitive
end cng
------------------------------------------------------------------------------
on doJavaScriptInChrome(jsCMD)
try
tell application "Google Chrome" to tell front window's active tab to execute javascript jsCMD
on error e
error "Error in handler doJavaScriptInChrome() of library NLb!" & return & return & e
end try
end doJavaScriptInChrome
------------------------------------------------------------------------------
on doJavaScriptInSafari(jsCMD)
try
tell application "Safari" to do JavaScript jsCMD in front document
on error e
error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
end try
end doJavaScriptInSafari
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
set e to e & return & return & "Num: " & n
if beepFlag = true then
beep
end if
if ddFlag = true then
tell application (path to frontmost application as text)
set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK" with icon 0
end tell
if button returned of dDlg = "Copy" then set the clipboard to e
else
return e
end if
end stdErr
------------------------------------------------------------------------------
-Chris