Skip to content

Instantly share code, notes, and snippets.

@mals14
mals14 / JXA Resources.md
Created October 15, 2021 16:00 — forked from JMichaelTX/JXA Resources.md
JavaScript for Automation (JXA) Resources
View JXA Resources.md

JXA Resources

Revised: 2019-11-28 16:16 GMT-6

JXA

This is a list of the key resources I have found useful. If you know of others, please post in a comment below, and I will add to this list.

I have tried to order this list in the order that, to me, is best for learning JXA from scratch. We all learn a bit diferently, so adjust to suit your style/needs. Please post if you have suggestions on learning JXA.

@mals14
mals14 / jxaClickAppSubMenuItem.applescript
Created December 2, 2020 13:42 — forked from bumaociyuan/jxaClickAppSubMenuItem.applescript
Yosemite JXA Javascript Function for clicking application sub-menu items
View jxaClickAppSubMenuItem.applescript
// Click an OS X app sub-menu item
// 2nd argument is an array of arbitrary length (exact menu item labels, giving full path)
// e.g. menuItemClick("InqScribe", ['View', 'Aspect Ratio', 'Use Media Ratio'])
// Note that the menu path (spelling & sequence) must be exactly as in the app
// See menuItemTestClick() below for a slower version which reports any errors
// For OS X 10.10 Yosemite JXA JavaScript for Automation
@mals14
mals14 / jxaClickAppSubMenuItem.applescript
Created December 2, 2020 13:40 — forked from RobTrew/jxaClickAppSubMenuItem.applescript
Yosemite JXA Javascript Function for clicking application sub-menu items
View jxaClickAppSubMenuItem.applescript
// Click an OS X app sub-menu item
// 2nd argument is an array of arbitrary length (exact menu item labels, giving full path)
// e.g. menuItemClick("InqScribe", ['View', 'Aspect Ratio', 'Use Media Ratio'])
// Note that the menu path (spelling & sequence) must be exactly as in the app
// See menuItemTestClick() below for a slower version which reports any errors
// For macOS Yosemite to Sierra
@mals14
mals14 / Get value at list or array index or "None" if out of range in Python.md
Last active June 25, 2020 17:50
Get value at list/array index or "None" if out of range in Python
View Get value at list or array index or "None" if out of range in Python.md

I find list slices good for this:

>>> x = [1, 2, 3]
>>> a = x [1:2]
>>> a
[2]
>>> b = x [4:5]
>>> b
[]
@mals14
mals14 / how to scroll a page with javascript.md
Created June 19, 2020 02:40
how to scroll a page with javascript
View how to scroll a page with javascript.md

How to Scroll a Page With JavaScript

Copied from: How to Scroll the Page With JavaScript Link

This page demonstrates how to use the JavaScript scrollBy and setTimeout methods to make a web page scroll down automatically. Change the timeout value to alter the scrolling speed in milliseconds. The example function below (arbitrarily called pageScroll) shows how this can work:

function pageScroll() {
    	window.scrollBy(0,50); // horizontal and vertical scroll increments
 scrolldelay = setTimeout('pageScroll()',100); // scrolls every 100 milliseconds
@mals14
mals14 / sleep pause capability in javascript.md
Last active June 19, 2020 02:33
sleep pause capability in javascript.md
View sleep pause capability in javascript.md

sourced from: What is the JavaScript version of sleep()? - Stack Overflow Link

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo() {
  console.log('Taking a break...');
  await sleep(2000);
 console.log('Two seconds later, showing sleep in a loop...');
@mals14
mals14 / example of how to insert a dictionary into sqlite database.py
Created June 14, 2020 16:15
example of how to insert a dictionary into sqlite database python
View example of how to insert a dictionary into sqlite database.py
# example of how to insert a dictionary into sqlite database
# suorce - https://stackoverflow.com/questions/10913080/python-how-to-insert-a-dictionary-to-a-sqlite-database
def post_row(conn, tablename, rec):
keys = ','.join(rec.keys())
question_marks = ','.join(list('?'*len(rec)))
values = tuple(rec.values())
conn.execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values)
@mals14
mals14 / how to quote for command line usage - library support in python.md
Last active June 14, 2020 13:40
how to quote for command line usage - library support in python
View how to quote for command line usage - library support in python.md

how to quote for command line usage - library support in python

  • shlex — Simple lexical analysis — Python 3.8.3 documentation Link
@mals14
mals14 / sort dictionaries in python.md
Created June 14, 2020 13:23
sort lists of dictionaries in python
View sort dictionaries in python.md

Sorting Lists of Dictionaries

Frequently you want to sort a list of dictionaries, based on some particular key.

Source: SortingListsOfDictionaries - Python Wiki Link

@mals14
mals14 / javascript examples - select based on xpath and extract text value.js
Last active June 13, 2020 14:52
javascript examples - select based on xpath and extract text value
View javascript examples - select based on xpath and extract text value.js
// javascript examples - select based on xpath and extract text value
var getXPath = '//div[contains(@class, "CopyToClipBoardInput")]/input';
var nodes = document.evaluate(getXPath, document, null, XPathResult.ANY_TYPE, null);
var getElem = nodes.iterateNext();
getElem.value // or is it getElem.valueOf
var getXPath = "//div[contains(@class, 'ContactInfoCallModal-phone')]//div";