I use KM actions shown below as part of a macro on a daily basis each morning to automatically advance three Google Sheet tabs (representing calendar schedules for three different locations). The workbooks are shared with me as "View Only", but I'm wondering if the sheet names (shown below also) can be read using Javascript or Applescript to automatically move to the correct sheet if my view gets out of sync with the current calendar date.
I know almost nothing about JavaScript myself, but I can tell you that this kind of JavaScript won't work in Script Editor. It has to be run either in a browser's JavaScript console (which you can find in the "hidden" Develop menu in Safari or the View > Developer menu in Chrome) or in a KM "Execute a JavaScript in Safari/Chrome/Front Browser" action.
Not realizing there was a difference, I typically test scripts in the Script Editor for syntax or when undesired and unsuccessful results are yielded when run in KM. I'll need to study the difference between the types of scripts and context from which they are run. Any recommended references?
Once your advice was heeded, I've obtained the results Chris warned of initially (inability to recognize active sheet). Thanks!.
The JSC Javascript interpreter used by Safari is the same as that used by Script Editor,
so no syntax differences, but:
JavaScript is an embedded scripting language, and the libraries and objects exposed to it depend on what application it's embedded in.
To test this, try evaluating console.log(Object.keys(this)) in the Safari or Chrome consoles, and then try the same thing in Script Editor. You will see a different set of objects in the global namespace.
(Chrome has a different JS interpreter, but it's still ES6 JS, so the syntax is the same, but again, only a browser's global namespace contains objects like document and window)
In the case of the code which your screenshot shows, the document object is a model of a parsed HTML page in a browser.
As Script Editor is not a browser, it can have no idea what the name document might be bound to.
The script that I provided could not possibly discern the active sheet, because it's just collating the names and returning them.
That doesn't mean it's not possible to discover the active sheet β I just don't know how.
Okay β you can get it with AppleScript and System Events, and it's pretty fast.
Ah, hell β you're using Chrome β this only works in Safari...
----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/05/12 10:18
# dMod: 2018/05/12 10:18
# Appl: Safari, System Events
# Task: Get name and position of Active Sheet of Google Sheets Spreadsheet.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @System_Events, @Name, @Position, @Active, @Sheet, @Google, @Sheets, @Spreadsheet
----------------------------------------------------------------
tell application "System Events"
tell application process "Safari"
tell (first window whose subrole is "AXStandardWindow")
tell group 1 of group 1 of tab group 1 of splitter group 1
tell scroll area 1
tell UI element 1
tell group 1
tell group 24
tell group 1
tell group 1
tell toolbar 1
set activeSheet to first pop up button whose attribute "AXDOMClassList"'s value contains "docs-sheet-active-tab"
tell activeSheet
set activeSheetName to its name
set activeSheetButtonPosition to value of attribute "AXPosition"
# its properties
# its attributes
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
----------------------------------------------------------------
Given this you can get the position of a button and have Keyboard Maestro click it if necessary.
It's a finagle that works (so far), but there's no telling how fragile it will be over time.
Well, I still don't know how to detect the active sheet (in Chrome).
But β I have discovered that you can record a macro in Google Sheets to go to the specific sheet you want β and you can assign a keyboard shortcut to the macro.
Look in the Sheets Menu Bar under:
Tools > Macros > Record Macro
It's pretty straightforward, so you should be able to figure it out.
Maybe @ComplexPoint will weigh-in on detecting the active sheet.
I appreciate the support from you guys. I'm still digging around the Internet, but unfortunately, I don't have sufficient knowledge to be able to use the Google Sheets API and Chris' script to get the current tab into a KM variable.
As far as I recall, the SpreadsheetApp object is only available inside a special Chrome plugin evaluation space, to which tools like KM can have no access.