One of my scripts is broken because the developer now generates a unique id and unique name for each button and field every time a page is launched.
In the example below I used to be able to populate the field by using the name “Headline” but that doesn’t work. If my script has the ID it works.
But I don’t know enough javascript to workaround this.
One solution: get the name and id (and other info) for every text area, and parse that with applescript to get the ID for the one that has the field name I want (in this case “/headline”)
Or a javascript that would work on with a partial match for the field name. (field who’s name contains “/headline”)
Or a javascript that could work with the “data-dynamic-field-name”
It looks like any of those would work (although I’d have to find the same kind of workaround for various buttons too.)
OK, so this is working. I’m using the partial label to grab the field ID, then using the field ID to fill in the field.
set storyHeadline to "Story Headline"
set storySlug to "Story Slug"
set storyIntialSlug to "Story Initial Slug"
set storySEO to "Story SEO"
set byLineButtonName to "addButton _ofc137 RCS-add"
set insertionInfo to {¬
{"textarea", storyHeadline, "headline"}, ¬
{"textarea", storySlug, "sluggable.slug"}, ¬
{"textarea", storyIntialSlug, "sluggable.internalSlug"}, ¬
{"textarea", storySEO, "subHeadline"}}
set AppleScript's text item delimiters to {""}
repeat with thisInfo in insertionInfo
set {fieldType, newContents, fieldLabel} to thisInfo as item
set jvScriptText to {"document.querySelector('"}
set the end of jvScriptText to fieldType
set the end of jvScriptText to "[name*=\"/"
set the end of jvScriptText to fieldLabel
set the end of jvScriptText to "\"]').id;"
set jvScriptText to jvScriptText as text
set elementId to RunJavaScript(jvScriptText)
InputByID(elementId, newContents)
end repeat
ClickByClass(byLineButtonName, 0)
InputByClass("ComboInput", 0, "Ed Stockly")
on RunJavaScript(jvScriptText)
tell application "Google Chrome"
activate
set jvScriptResult to execute (active tab of window 1) javascript jvScriptText
end tell
return jvScriptResult
end RunJavaScript
on ClickByName(objectName, elementnum)
set jvScriptText to "document.getElementsByName('" & objectName & "')[" & elementnum & "].click();"
set jvScriptResult to RunJavaScript(jvScriptText)
return jvScriptResult
end ClickByName
on ClickByClass(objectClass, elementnum)
set jvScriptText to "document.getElementsByClassName('" & objectClass & "')[" & elementnum & "].click();"
set jvScriptResult to RunJavaScript(jvScriptText)
return jvScriptResult
end ClickByClass
on InputByIDfromName(theName, num, theValue)
set theValue to EscapeSingleQuote(theValue)
set InputByIDfromNameJVscript to "document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "'"
set jvScriptResult to RunJavaScript(InputByIDfromNameJVscript)
return jvScriptResult
end InputByIDfromName
on InputByName(theName, num, theValue)
set theValue to EscapeSingleQuote(theValue)
set InputByNameJVscript to "document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" & return
tell application "Google Chrome"
set jvScriptResult to execute (active tab of window 1) javascript InputByNameJVscript
end tell
return jvScriptResult
end InputByName
on InputByID(theId, theValue)
set theValue to EscapeSingleQuote(theValue)
set jvScriptText to "document.getElementById('" & theId & "').value ='" & theValue & "'"
set jvScriptResult to RunJavaScript(jvScriptText)
end InputByID
on InputByClass(theClass, theIndex, theValue)
set theValue to EscapeSingleQuote(theValue)
set InputByClassJVscript to "document.getElementsByClassName('" & theClass & "')[" & theIndex & "].value ='" & theValue & "'"
set jvScriptResult to RunJavaScript(InputByClassJVscript)
return jvScriptResult
end InputByClass
on EscapeSingleQuote(textToFix)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"'"}
set textToFix to every text item of textToFix
set AppleScript's text item delimiters to {"\\'"}
set textToFix to textToFix as text
set AppleScript's text item delimiters to saveTID
return textToFix
end EscapeSingleQuote