I am trying to extract the name and player ID from the LPGA web site. I got as far as extracting the first name. But the use of the Applescript text item delimeter to move through text is hard for me to follow (still new at this). My current code finds the first person three times.
The web page has code that looks like:
Here is my script. I initially find “PlayerListingControl_AlphaImage” which is close to the text I need. Then I work on the “player_results.aspx?id=” to get the PlayerID, followed by the Player Name. I included all the script in case that helps, I apologize if I should have only included the main loop.
set astid to AppleScript's text item delimiters -- for later reference by variable. It is always good practice to store these and finally set them back. AppleScript's text item delimiters are a global property of AppleScript so if you leave them in a strange setting, other scripts open at the same time will respond to them.
--set cond_start to the text in the web page just before the value, here it is Greensheet labels
set cond_start to "PlayerListingControl_AlphaImage"
set next_cond to "player_results.aspx?id="
set downloadURL to "http://www.lpga.com/player_results.aspx?alpha="
set AllRank to "PlayerID,Last Name,First Name" & return
set alpha to "a"
-- Curl command downloads the web page into variable T
set curlCode to "curl '" & downloadURL & alpha & "'"
set T to (do shell script curlCode)
set AppleScript's text item delimiters to cond_start --find PlayerListingControl_AlphaImage
set lastPart to text item 2 of T -- keep the last part, where the data is located
repeat with m from 1 to 3
set AppleScript's text item delimiters to next_cond -- player_results.aspx?id=
set lastPart to text item 2 of T
set AppleScript's text item delimiters to "'>"
--Getting Player ID
set PlayerID to text item 1 of lastPart
set lastPart to text item 2 of lastPart
---Getting Player's Name
set AppleScript's text item delimiters to "</a>"
set Mystring to text item 1 of lastPart -- Mystring now has player name with tabs and return
ParsePlayername(Mystring)
set Playername to result
set AllRank to AllRank & PlayerID & "," & Playername & return
end repeat
tell application "TextEdit"
activate
make new document at beginning of documents
set text of document 1 to AllRank & return
end tell
--Resetting the text delimiter for Applescript to the default.
set AppleScript's text item delimiters to astid
--Function to clear off tabs and returns
on ParsePlayername(Mystring)
set AppleScript's text item delimiters to " "
set thenewlist to every text item in Mystring
set AppleScript's text item delimiters to ""
set notablist to every item in thenewlist as string --notablist has player name with no tabs
set AppleScript's text item delimiters to return
set thesecondlist to every text item in notablist
set AppleScript's text item delimiters to ""
set cleanName to every item in thesecondlist as string --Playername should have no tabs or return
return cleanName
end ParsePlayername
You can see I have been reading some of your tutorials and using code liberally. I have just begun to use Applescript, I have done some Excel macro programming and many decades ago I could program Fortran. But not very experienced.