AppleScript: Word count 2

Hi,

Marked my previous Q AppleScript: word count as solved before testing it. the answer that was given compiled correctly but didn't run. Instead produced error "Google Chrome got an error: Can’t make 2 into type specifier."


if (countwords of wordStr) is greater than 2 then

set vol to word 2 of textNeeded

tell application "Numbers" to activate

tell application "System Events"

tell process "Numbers"

keystroke "["

keystrokevol

keystroke "]"

keystroke " "

end tell

end tell

end if


So I played around with the script. trying:


if (countwords of wordStr) is greater than (count 2) then

if (countwords of wordStr) is greater than (count word 2) then

if (countwords of wordStr) is greater than (count of 2) then

with and without brackets.

and mostly got the same error. one variation produced the error "Google Chrome got an error: Can’t get word 2.".

What am I doing wrong? If it helps I can paste the whole script otherwise let's hope it can be solved.

Thanks

MacBook Pro with Retina display, macOS High Sierra (10.13.4)

Posted on Apr 18, 2018 12:47 AM

Reply
5 replies

Apr 19, 2018 4:53 AM in response to lgp007

I can't really follow your script, but I can get you to where you want (but using Safari, though you could probably do the same thing with Chrome's JS support). (Load the page in Safari and keep it as the frontmost document, then paste this into Script Editor and run.)


I've put some comments in to show you what's happening


tell application "Safari"

set theURL to URL of document 1 --gets the URL which contains the wiki ID

set oldTID to AppleScript'stext item delimiters--stores current TID value

set AppleScript'stext item delimiters to "/" --sets TID to "/"

set wikiId to text item -2 of theURL--puts the second to last text item of the URL (as broken-up by "/") into wikiId

set AppleScript'stext item delimiters to oldTID--restores TID to previous value

set theId to "wiki-" & wikiId & "-deck" --build the wiki string for JavaScript

set theVolume to do JavaScript "document.getElementById('" & theId & "').innerText" in document 1

set theVolume to words of theVolume--breaks up by words

set theVolume to second item of theVolume --gets second word


--iterates over all elements with tag name 'a' looking for "wiki-title"

repeat with x from 0 to ((do JavaScript "document.getElementsByTagName('a').length" in document 1) - 1)

set theTitle to do JavaScript "document.getElementsByTagName('a')[" & x & "].outerHTML" in document 1

if theTitle contains "wiki-title" then exit repeat --when it's found exit the loop

end repeat

set theTitle to do JavaScript "document.getElementsByTagName('a')[" & x & "].innerHTML" in document 1 --get the actual title using the found value of 'x'

set theResult to theTitle & " [v" & theVolume & "] (" & wikiId & ")" --build the whole return string

end tell

Apr 19, 2018 2:17 AM in response to Arkouda

Hi Arkouda,


Thanks for answering. Below is the whole script. and my aim is to isolate the volume number eg 3 out of "Volume 3" on this site (https://comicvine.gamespot.com/s-h-i-e-l-d/4050-79159/) and paste it into an active cell in numbers. There are other scripts that get the name and the digit code of the page, which I hope to integrate this script into so that the whole result will be "S.H.I.E.L.D. [v3] (4050-79159)" for that page.


Thank you for you help


tell application "Google Chrome"

view source of active tab of first window

tell application "Google Chrome" to activate

delay 3.0

tell application "System Events"

tell process "Google Chrome"

keystroke "a" usingcommand down

keystroke "c" usingcommand down

delay 1.0

keystroke "w" usingcommand down

end tell

end tell


set textNeeded to my extractBetween(the clipboard, "<h3 class=\"display-view\">", "</h3>")

set wordStr to countwords of textNeeded

if countwords of wordStr is greater than (countword 2) then

set vol to word 2 of textNeeded

tell application "Numbers" to activate

tell application "System Events"

tell process "Numbers"

keystroke "["

keystrokevol

keystroke "]"

keystroke " "

end tell

end tell

--if (count words of textNeeded) is less than 2 then

-- tell application "Numbers" to activate

-- tell application "System Events"

-- tell process "Numbers"

-- keystroke " "

-- end tell

-- end tell

--end if

end if

end tell


on extractBetween(searchText, startText, endText)

set defaultDelims to AppleScript'stext item delimiters

set AppleScript'stext item delimiters to startText

if (count of text items of searchText) is 1 then return "no start" -- no match for startText

set searchText to second text item of searchText

set AppleScript'stext item delimiters to endText

if (count of text items of searchText) is 1 then return "no end" -- no match for endText

set searchText to first text item of searchText

set AppleScript'stext item delimiters to defaultDelims

return searchText

end extractBetween

May 1, 2018 2:01 AM in response to Arkouda

Sorry to use this post to contact someone, but couldn’t find another way.... trying to reach out to Arkouda


Arkouda, was reading your post about Move Mailing List Emails with Rule and I’m curious about your AppleScript solution, could you reach out to me on how you did it with AppleScript , here is a post Move mailing list emails with rule (AppleScript) I setup , so I don’t have to hijack this nice fellas post.


Again sorry for posting this non related post to get in touch with Arkouda.


regards

scot

Apr 18, 2018 11:22 PM in response to lgp007

It's confusing without the whole script, or at least without the whole Google Chrome block.


It's unclear why you are checking the word count of one string (wordStr), but then getting the second word from another (textNeeded). Perhaps that's where you need to look?


Perhaps your problem is because your check is wrong: for a string with two or fewer words, you should be saying "is greater than 1". (If you pass a string/s with a length of two words to your current code, the if test will not pass, so vol will not be set to anything.)


A bit more explanation of the strings you're using and what you're trying to achieve would be helpful!

Apr 19, 2018 6:33 PM in response to Arkouda

Thanks, Arkouda,


The script you provided works great in Safari.


Below I've provided the Google Chrome version of Arkouda's script.


tell application "Google Chrome"

set theURL to URL of active tab of first window

set oldTID to AppleScript'stext item delimiters

set AppleScript'stext item delimiters to "/"

set wikiId to text item -2 of theURL

set AppleScript'stext item delimiters to oldTID

set theId to "wiki-" & wikiId & "-deck"

set theVolume to executeactive tab of first windowjavascript "document.getElementById('" & theId & "').innerText"

set theVolume to words of theVolume

set theVolume to second item of theVolume

repeat with x from 0 to ((executeactive tab of first windowjavascript "document.getElementsByTagName('a').length") - 1)

set theTitle to executeactive tab of first windowjavascript "document.getElementsByTagName('a')[" & x & "].outerHTML"

if theTitle contains "wiki-title" then exit repeat

end repeat

set theTitle to executeactive tab of first windowjavascript "document.getElementsByTagName('a')[" & x & "].innerHTML"

set theResult to theTitle & " [v" & theVolume & "] (" & wikiId & ")"

end tell


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript: Word count 2

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.