Advertisement
Guest User

Batch Create

a guest
Sep 30th, 2014
4,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2. code to find all elements on iTunes page, for use with "verifyPage()"
  3.  
  4. tell application "System Events"
  5.     set elementCount to count of every UI element of UI element 1 of scroll area 3 of window 1 of application process "iTunes"
  6.     set everyElement to every UI element of UI element 1 of scroll area 3 of window 1 of application process "iTunes"
  7.  
  8.     set everyProperty to {}
  9.     repeat with loopCounter from 1 to (count of items in everyElement)
  10.         try
  11.             set everyProperty to everyProperty & 1
  12.             set item loopCounter of everyProperty to (properties of item loopCounter of everyElement)
  13.         end try
  14.     end repeat
  15.  
  16.     set everyTitle to {}
  17.     repeat with loopCounter from 1 to (count of items in everyProperty)
  18.         set everyTitle to everyTitle & ""
  19.         try
  20.             set item loopCounter of everyTitle to (title of item loopCounter of everyProperty)
  21.         end try
  22.     end repeat
  23.  
  24. end tell
  25.  
  26. *)
  27.  
  28. --TO DO:
  29.  
  30. --write itunes running check
  31. --write file output section for account status column
  32. --write check for account status of "completed" or "skipped"
  33.  
  34. --Global Vars
  35.  
  36. --Used for storing a list of encountered errors. Written to by various handlers, read by checkForErrors()
  37. global errorList
  38. set errorList to {}
  39.  
  40. --Used for controlling the running or abortion of the script. Handler will run as long as scriptAction is "Continue". Can also be set to "Abort" to end script, or "Skip User" to skip an individual user.
  41. global scriptAction
  42. set scriptAction to "Continue"
  43.  
  44. --Store the current user number (based off line number in CSV file)
  45. global currentUser
  46. set currentUserNumber to 0
  47.  
  48. --Used for completing every step in the process, except actually creating the Apple ID. Also Pauses the script at various locations so the user can verify everything is working properly.
  49. property dryRun : true
  50.  
  51. --Used to store the file location of the iBooks "App Page Shortcut". Updated dynamically on run to reference a child folder of the .app bundle (Yes, I know this isn't kosher)
  52. -- AF 2012-05-14 Open location instead of .inetloc
  53. property ibooksLinkLocation : "itms://itunes.apple.com/us/app/ibooks/id364709193?mt=8"
  54.  
  55. --Master delay timer for slowing the script down at specified sections. Usefull for tweaking the entire script's speed
  56. property masterDelay : 1
  57.  
  58. --Maximum time (in seconds) the script will wait for a page to load before giving up and throwing an error
  59. property netDelay : 30
  60.  
  61. --Used at locations in script that will be vulnerable to slow processing. Multiplied by master delay. Tweak for slow machines. May be added to Net Delay.
  62. property processDelay : 1
  63.  
  64. --How often should the script check that something has loaded/is ready
  65. property checkFrequency : 0.5
  66.  
  67. --Used to store supported iTunes versions
  68. property supportedItunesVersions : {"11.2.2", "11.3", "11.3.1", "11.4"}
  69.  
  70. --Used for checking if iTunes is loading a page
  71. property itunesAccessingString : "Accessing iTunes Storeā€¦"
  72.  
  73. (*
  74.     Email
  75.     Password
  76.     Secret Question 1
  77.     Secret Answer 1
  78.     Secret Question 2
  79.     Secret Answer 2
  80.     Secret Question 3
  81.     Secret Answer 3
  82.     Month Of Birth
  83.     Day Of Birth
  84.     Year Of Birth
  85.     First Name
  86.     Last Name
  87.     Address Street
  88.     Address City
  89.     Address State
  90.     Address Zip
  91.     Phone Area Code
  92.     Phone Number
  93.     Account Status
  94. *)
  95.  
  96. --Properties for storing possible headers to check the source CSV file for. Source file will be checked for each of the items to locate the correct columns
  97. property emailHeaders : {"Email", "Email Address"}
  98. property passwordHeaders : {"Password", "Pass"}
  99. property secretQuestion1Headers : {"Secret Question 1"}
  100. property secretAnswer1Headers : {"Secret Answer 1"}
  101. property secretQuestion2Headers : {"Secret Question 2"}
  102. property secretAnswer2Headers : {"Secret Answer 2"}
  103. property secretQuestion3Headers : {"Secret Question 3"}
  104. property secretAnswer3Headers : {"Secret Answer 3"}
  105. property monthOfBirthHeaders : {"Month", "Birth Month", "Month of Birth"}
  106. property dayOfBirthHeaders : {"Day", "Birth Day", "Day Of Birth"}
  107. property yearOfBirthHeaders : {"Year", "Birth Year", "Year Of Birth"}
  108. property firstNameHeaders : {"First Name", "First", "fname"}
  109. property lastNameHeaders : {"Last Name", "Last", "lname"}
  110. property addressStreetHeaders : {"Street", "Street Address", "Address Street"}
  111. property addressCityHeaders : {"City", "Address City"}
  112. property addressStateHeaders : {"State", "Address State"}
  113. property addressZipHeaders : {"Zip Code", "Zip", "Address Zip"}
  114. property phoneAreaCodeHeaders : {"Area Code", "Phone Area Code"}
  115. property phoneNumberHeaders : {"Phone Number", "Phone"}
  116. property rescueEmailHeaders : {"Rescue Email (Optional)", "Rescue Email"}
  117. property accountStatusHeaders : {"Account Status"} --Used to keep track of what acounts have been created
  118.  
  119.  
  120. set userDroppedFile to false
  121.  
  122. --Check to see if a file was dropped on this script
  123. on open droppedFile
  124.     set userDroppedFile to true
  125.     MainMagic(userDroppedFile, droppedFile)
  126. end open
  127.  
  128. --Launch the script in interactive mode if no file was dropped (if file was dropped on script, this will never be run, because of the "on open" above)
  129. set droppedFile to ""
  130. MainMagic(userDroppedFile, droppedFile)
  131.  
  132. on MainMagic(userDroppedFile, droppedFile)
  133.     --CHECK ITUNES SUPPORT-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CHECK ITUNES SUPPORT--
  134.    
  135.     set itunesVersion to version of application "iTunes"
  136.     set itunesVersionIsSupported to false
  137.    
  138.     repeat with versionCheckLoopCounter from 1 to (count of items in supportedItunesVersions)
  139.         if item versionCheckLoopCounter of supportedItunesVersions is equal to itunesVersion then
  140.             set itunesVersionIsSupported to true
  141.             exit repeat
  142.         end if
  143.     end repeat
  144.    
  145.     if itunesVersionIsSupported is false then
  146.         set scriptAction to button returned of (display dialog "iTunes is at version " & itunesVersion & return & return & "It is unknown if this version of iTunes will work with this script." & return & return & "You may abort now, or try running the script anyway." buttons {"Abort", "Continue"} default button "Abort") as text
  147.     end if
  148.    
  149.     if scriptAction is "Continue" then
  150.         --LOAD USERS FILE-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------LOAD USERS FILE--
  151.        
  152.         set usersFile to loadUsersFile(userDroppedFile, droppedFile) --Load the users file. Returns a list of columns from the source file
  153.        
  154.         if scriptAction is "Continue" then
  155.             --Split out header information from each of the columns
  156.             set headers to {}
  157.            
  158.             repeat with headerRemoverLoopCounter from 1 to (count of items in usersFile)
  159.                
  160.                 set headers to headers & "" --Add an empty item to headers
  161.                
  162.                 set item headerRemoverLoopCounter of headers to item 1 of item headerRemoverLoopCounter of usersFile --Save the header from the column
  163.                
  164.                 set item headerRemoverLoopCounter of usersFile to (items 2 thru (count of items in item headerRemoverLoopCounter of usersFile) of item headerRemoverLoopCounter of usersFile) --Remove the header from the column
  165.                
  166.             end repeat
  167.            
  168.             set userCount to (count of items in item 1 of usersFile) --Counts the number of users
  169.            
  170.             --seperated column contents (not really necessarry, but it makes everything else a whole lot more readable)
  171.             set appleIdEmailColumnContents to item 1 of usersFile
  172.             set appleIdPasswordColumnContents to item 2 of usersFile
  173.            
  174.             set appleIdSecretQuestion1ColumnContents to item 3 of usersFile
  175.             set appleIdSecretAnswer1ColumnContents to item 4 of usersFile
  176.             set appleIdSecretQuestion2ColumnContents to item 5 of usersFile
  177.             set appleIdSecretAnswer2ColumnContents to item 6 of usersFile
  178.             set appleIdSecretQuestion3ColumnContents to item 7 of usersFile
  179.             set appleIdSecretAnswer3ColumnContents to item 8 of usersFile
  180.             set monthOfBirthColumnContents to item 9 of usersFile
  181.             set dayOfBirthColumnContents to item 10 of usersFile
  182.             set yearOfBirthColumnContents to item 11 of usersFile
  183.            
  184.             set userFirstNameColumnContents to item 12 of usersFile
  185.             set userLastNameColumnContents to item 13 of usersFile
  186.             set addressStreetColumnContents to item 14 of usersFile
  187.             set addressCityColumnContents to item 15 of usersFile
  188.             set addressStateColumnContents to item 16 of usersFile
  189.             set addressZipColumnContents to item 17 of usersFile
  190.             set phoneAreaCodeColumnContents to item 18 of usersFile
  191.             set phoneNumberColumnContents to item 19 of usersFile
  192.             set appleIdRescueColumnContents to item 20 of usersFile
  193.             set accountStatusColumnContents to item 21 of usersFile
  194.            
  195.            
  196.             --PREP-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------PREP--
  197.            
  198.             --Ask user if they want to perform a dry run, and give them a chance to cancel
  199.             set scriptRunMode to button returned of (display dialog "Would you like to preform a ''dry run'' of the script?" & return & return & "A ''dry run'' will run through every step, EXCEPT actually creating the Apple IDs." buttons {"Actually Create Apple IDs", "Dry Run", "Cancel"}) as text
  200.             if scriptRunMode is "Actually Create Apple IDs" then set dryRun to false
  201.             if scriptRunMode is "Dry Run" then set dryRun to true
  202.             if scriptRunMode is "Cancel" then set scriptAction to "Abort"
  203.            
  204.             --CREATE IDS-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CREATE IDS--
  205.             if scriptAction is not "Abort" then
  206.                 set accountStatusSetByCurrentRun to {}
  207.                 set currentUserNumber to 0
  208.                 repeat with loopCounter from 1 to userCount
  209.                    
  210.                     --Increment our current user, just so other handlers can know what user we are on
  211.                     set currentUserNumber to currentUserNumber + 1
  212.                    
  213.                     --Get a single user's information from the column contents
  214.                     set appleIdEmail to item loopCounter of appleIdEmailColumnContents
  215.                     set appleIdPassword to item loopCounter of appleIdPasswordColumnContents
  216.                    
  217.                     set appleIdSecretQuestion1 to item loopCounter of appleIdSecretQuestion1ColumnContents
  218.                     set appleIdSecretAnswer1 to item loopCounter of appleIdSecretAnswer1ColumnContents
  219.                     set appleIdSecretQuestion2 to item loopCounter of appleIdSecretQuestion2ColumnContents
  220.                     set appleIdSecretAnswer2 to item loopCounter of appleIdSecretAnswer2ColumnContents
  221.                     set appleIdSecretQuestion3 to item loopCounter of appleIdSecretQuestion3ColumnContents
  222.                     set appleIdSecretAnswer3 to item loopCounter of appleIdSecretAnswer3ColumnContents
  223.                     set rescueEmail to item loopCounter of appleIdRescueColumnContents
  224.                     set monthOfBirth to item loopCounter of monthOfBirthColumnContents
  225.                     set dayOfBirth to item loopCounter of dayOfBirthColumnContents
  226.                     set yearOfBirth to item loopCounter of yearOfBirthColumnContents
  227.                    
  228.                     set userFirstName to item loopCounter of userFirstNameColumnContents
  229.                     set userLastName to item loopCounter of userLastNameColumnContents
  230.                     set addressStreet to item loopCounter of addressStreetColumnContents
  231.                     set addressCity to item loopCounter of addressCityColumnContents
  232.                     set addressState to item loopCounter of addressStateColumnContents
  233.                     set addressZip to item loopCounter of addressZipColumnContents
  234.                     set phoneAreaCode to item loopCounter of phoneAreaCodeColumnContents
  235.                     set phoneNumber to item loopCounter of phoneNumberColumnContents
  236.                     set accountStatus to item loopCounter of accountStatusColumnContents
  237.                    
  238.                     delay masterDelay
  239.                    
  240.                     SignOutItunesAccount() ---------------------------------------------------------------------------------------------------------------------------------------------------------Signout Apple ID that is currently signed in (if any)
  241.                    
  242.                     --delay 10
  243.                    
  244.                     installIbooks() ---------------------------------------------------------------------------------------------------------------------------------------------------------------------Go to the iBooks App page location to kick off Apple ID creation with no payment information
  245.                    
  246.                     delay 1 --Fix so iTunes is properly tested for, instead of just manually delaying
  247.                    
  248.                     repeat
  249.                         set lcdStatus to GetItunesStatusUntillLcd("Does Not Match", itunesAccessingString, 4, "times. Check for:", 120, "intervals of", 0.25, "seconds") ------------------------Wait for iTunes to open (if closed) and the iBooks page to load
  250.                         if lcdStatus is "Matched" then exit repeat
  251.                         delay masterDelay
  252.                     end repeat
  253.                    
  254.                    
  255.                     CheckForErrors() ------------------------------------------------------------------------------------------------------------------------------------------------------------------Checks for errors that may have been thrown by previous handler
  256.                     if scriptAction is "Abort" then exit repeat -----------------------------------------------------------------------------------------------------------------------------------If an error was detected and the user chose to abort, then end the script
  257.                    
  258.                     ClickCreateAppleIDButton() -----------------------------------------------------------------------------------------------------------------------------------------------------Click "create Apple ID" button on pop-up window
  259.                     ClickContinueOnPageOne() ------------------------------------------------------------------------------------------------------------------------------------------------------Click "Continue" on the page with the title "Welcome to the iTunes Store"
  260.                     CheckForErrors() ------------------------------------------------------------------------------------------------------------------------------------------------------------------Checks for errors that may have been thrown by previous handler
  261.                     if scriptAction is "Abort" then exit repeat -----------------------------------------------------------------------------------------------------------------------------------If an error was detected and the user chose to abort, then end the script
  262.                    
  263.                     AgreeToTerms() -------------------------------------------------------------------------------------------------------------------------------------------------------------------Check the "I have read and agreed" box and then the "Agree" button
  264.                     CheckForErrors() ------------------------------------------------------------------------------------------------------------------------------------------------------------------Checks for errors that may have been thrown by previous handler
  265.                     if scriptAction is "Abort" then exit repeat -----------------------------------------------------------------------------------------------------------------------------------If an error was detected and the user chose to abort, then end the script
  266.                    
  267.                     log {"Creating ", appleIdEmail}
  268.                    
  269.                     ProvideAppleIdDetails(appleIdEmail, appleIdPassword, appleIdSecretQuestion1, appleIdSecretAnswer1, appleIdSecretQuestion2, appleIdSecretAnswer2, appleIdSecretQuestion3, appleIdSecretAnswer3, rescueEmail, monthOfBirth, dayOfBirth, yearOfBirth) ----------------Fills the first page of apple ID details. Birth Month is full text, like "January". Birth Day and Birth Year are numeric. Birth Year is 4 digit
  270.                     CheckForErrors() ------------------------------------------------------------------------------------------------------------------------------------------------------------------Checks for errors that may have been thrown by previous handler
  271.                     if scriptAction is "Abort" then exit repeat -----------------------------------------------------------------------------------------------------------------------------------If an error was detected and the user chose to abort, then end the script
  272.                    
  273.                     ProvidePaymentDetails(userFirstName, userLastName, addressStreet, addressCity, addressState, addressZip, phoneAreaCode, phoneNumber) -------------Fill payment details, without credit card info
  274.                     CheckForErrors() ------------------------------------------------------------------------------------------------------------------------------------------------------------------Checks for errors that may have been thrown by previous handler
  275.                     if scriptAction is "Abort" then exit repeat -----------------------------------------------------------------------------------------------------------------------------------If an error was detected and the user chose to abort, then end the script
  276.                    
  277.                     if scriptAction is "Continue" then ----------------------------------------------------------------------------------------------------------------------------------------------If user was successfully created...
  278.                         set accountStatusSetByCurrentRun to accountStatusSetByCurrentRun & ""
  279.                         set item loopCounter of accountStatusSetByCurrentRun to "Created" ----------------------------------------------------------------------------------------------Mark user as created
  280.                     end if
  281.                    
  282.                     if scriptAction is "Skip User" then ----------------------------------------------------------------------------------------------------------------------------------------------If a user was skipped...
  283.                         set accountStatusSetByCurrentRun to accountStatusSetByCurrentRun & ""
  284.                         set item loopCounter of accountStatusSetByCurrentRun to "Skipped" ----------------------------------------------------------------------------------------------Mark user as "Skipped"
  285.                         set scriptAction to "Continue" ----------------------------------------------------------------------------------------------------------------------------------------------Set the Script back to "Continue" mode
  286.                     end if
  287.                    
  288.                     if scriptAction is "Stop" then exit repeat
  289.                    
  290.                 end repeat
  291.                
  292.                 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Display dialog boxes that confirm the exit status of the script
  293.                
  294.                 activate
  295.                 if scriptAction is "Abort" then display dialog "Script was aborted" buttons {"OK"}
  296.                 if scriptAction is "Stop" then display dialog "Dry run completed" buttons {"OK"}
  297.                 if scriptAction is "Continue" then display dialog "Script Completed Successfully" buttons {"OK"}
  298.                
  299.                
  300.                 --Fix for multiple positive outcomes
  301.                 if itunesVersionIsSupported is false then --If the script was run against an unsupported version of iTunes...
  302.                     if scriptAction is "Continue" then --And it wasn't aborted...
  303.                         if button returned of (display dialog "Would you like to add iTunes Version " & itunesVersion & " to the list of supported iTunes versions?" buttons {"Yes", "No"} default button "No") is "Yes" then --...then ask the user if they want to add the current version of iTunes to the supported versions list
  304.                             set supportedItunesVersions to supportedItunesVersions & itunesVersion
  305.                             display dialog "iTunes version " & itunesVersion & " succesfully added to list of supported versions."
  306.                         end if
  307.                     end if
  308.                 end if
  309.             end if
  310.         end if
  311.     end if
  312.     -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------End main function
  313.    
  314. end MainMagic
  315.  
  316. (*_________________________________________________________________________________________________________________________________________*)
  317.  
  318. --FUNCTIONS-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------FUNCTIONS--
  319.  
  320. on loadUsersFile(userDroppedFile, chosenFile)
  321.     if userDroppedFile is false then set chosenFile to "Choose"
  322.     set readFile to ReadCsvFile(chosenFile) --Open the CSV file and read its raw contents
  323.     set readFile to ParseCsvFile(readFile) --Parse the values into a list of lists
  324.    
  325.     set listOfColumnsToFind to {"Email", "Password", "Secret Question 1", "Secret Answer 1", "Secret Question 2", "Secret Answer 2", "Secret Question 3", "Secret Answer 3", "Month Of Birth", "Day Of Birth", "Year Of Birth", "First Name", "Last Name", "Address Street", "Address City", "Address State", "Address Zip", "Phone Area Code", "Phone Number", "Rescue Email (Optional)", "Account Status"}
  326.    
  327.     --Locate the columns in the file
  328.     set findResults to {}
  329.     repeat with columnFindLoopCounter from 1 to (count of items in listOfColumnsToFind)
  330.         set findResults to findResults & ""
  331.         set item columnFindLoopCounter of findResults to findColumn((item columnFindLoopCounter of listOfColumnsToFind), readFile) --FindColumn Returns a list of two items. The first item is either "Found" or "Not Found". The second item (if the item was "found") will be a numerical reference to the column that was found, based on its position in the source file
  332.     end repeat
  333.    
  334.     --Verify that the columns were found, and resolve any missing columns
  335.     repeat with columnVerifyLoopCounter from 1 to (count of items in findResults)
  336.         if scriptAction is "Continue" then
  337.             if item 1 of item columnVerifyLoopCounter of findResults is "Found" then --Check if the current item to be located was found
  338.                 set item columnVerifyLoopCounter of findResults to item 2 of item columnVerifyLoopCounter of findResults --Remove the verification information and set the item to just the column number
  339.             else --If a column is missing
  340.                 --Ask the user what they would like to do
  341.                 set missingColumnResolution to button returned of (display dialog "The script was unable to locate the " & item columnVerifyLoopCounter of listOfColumnsToFind & " column. The script cannot continue without this information." & return & return & "What would you like to do?" buttons {"Abort Script", "Manually Locate Column"}) as text
  342.                
  343.                 --If the user chose to abort
  344.                 if missingColumnResolution is "Abort Script" then set scriptAction to "Abort"
  345.                
  346.                 --If the user chose to manually locate the column
  347.                 if missingColumnResolution is "Manually Locate Column" then
  348.                     --Create a list of the columns to choose from, complete with a number at the beginning of each item in the list
  349.                     set columnList to {}
  350.                     repeat with createColumnListLoopCounter from 1 to (count of items in readFile) --Each loop will create an entry in the list of choices corresponding to the first row of a column in the original source file
  351.                         set columnList to columnList & ((createColumnListLoopCounter as text) & " " & item 1 of item createColumnListLoopCounter of readFile) --Dynamically add an incremented number and space to the beginning of each item in the list of choices, and then add the contents of the first row of the column chosen for this loop
  352.                     end repeat
  353.                    
  354.                     --Present the list of column choices to the user
  355.                     set listChoice to choose from list columnList with prompt "Which of the items below is an example of ''" & item columnVerifyLoopCounter of listOfColumnsToFind & "''" --Ask user which of the choices matches what we are looking for
  356.                     if listChoice is false then --If the user clicked cancel in the list selection dialog box
  357.                         set scriptAction to "Abort"
  358.                     else
  359.                         set item columnVerifyLoopCounter of findResults to (the first word of listChoice as number) --Set the currently evaluating entry of findResults to the column NUMBER (determined by getting the first word of list choice, which corresponds to column numbers) the user selected
  360.                     end if
  361.                 end if
  362.                
  363.             end if
  364.         else --If an abort has been thrown
  365.             exit repeat
  366.         end if
  367.     end repeat
  368.    
  369.     --Retrieve the contents of the found columns
  370.     if scriptAction is "Continue" then
  371.         set fileContents to {}
  372.         repeat with contentRetrievalLoopCounter from 1 to (count of items in findResults)
  373.             set fileContents to fileContents & ""
  374.             set item contentRetrievalLoopCounter of fileContents to getColumnContents((item contentRetrievalLoopCounter of findResults), readFile)
  375.         end repeat
  376.     end if
  377.    
  378.     if scriptAction is "Continue" then
  379.         return fileContents
  380.     end if
  381.    
  382. end loadUsersFile
  383.  
  384. on findColumn(columnToFind, fileContents)
  385.    
  386.     --BEGIN FIND EMAIL                                                                                          BEGIN FIND EMAIL
  387.     if columnToFind is "Email" then
  388.         return findInList(emailHeaders, fileContents)
  389.     end if
  390.    
  391.     --BEGIN FIND PASSWORD                                                                                       BEGIN FIND PASSWORD
  392.     if columnToFind is "Password" then
  393.         return findInList(passwordHeaders, fileContents)
  394.     end if
  395.    
  396.     --BEGIN FIND SECRET QUESTION                                                                                BEGIN FIND SECRET QUESTION
  397.     if columnToFind is "Secret Question 1" then
  398.         return findInList(secretQuestion1Headers, fileContents)
  399.     end if
  400.    
  401.     --BEGIN FIND SECRET ANSWER                                                                                  BEGIN FIND SECRET ANSWER
  402.     if columnToFind is "Secret Answer 1" then
  403.         return findInList(secretAnswer1Headers, fileContents)
  404.     end if
  405.    
  406.     --BEGIN FIND SECRET QUESTION 2                                                                              BEGIN FIND SECRET QUESTION 2
  407.     if columnToFind is "Secret Question 2" then
  408.         return findInList(secretQuestion2Headers, fileContents)
  409.     end if
  410.    
  411.     --BEGIN FIND SECRET ANSWER 2                                                                                    BEGIN FIND SECRET ANSWER 2
  412.     if columnToFind is "Secret Answer 2" then
  413.         return findInList(secretAnswer2Headers, fileContents)
  414.     end if
  415.    
  416.     --BEGIN FIND SECRET QUESTION  3                                                                             BEGIN FIND SECRET QUESTION 3
  417.     if columnToFind is "Secret Question 3" then
  418.         return findInList(secretQuestion3Headers, fileContents)
  419.     end if
  420.    
  421.     --BEGIN FIND SECRET ANSWER 3                                                                                    BEGIN FIND SECRET ANSWER 3
  422.     if columnToFind is "Secret Answer 3" then
  423.         return findInList(secretAnswer3Headers, fileContents)
  424.     end if
  425.    
  426.     --BEGIN FIND BIRTH MONTH                                                                                    BEGIN FIND BIRTH MONTH
  427.     if columnToFind is "Month Of Birth" then
  428.         return findInList(monthOfBirthHeaders, fileContents)
  429.     end if
  430.    
  431.     --BEGIN FIND BIRTH DAY                                                                                      BEGIN FIND BIRTH DAY
  432.     if columnToFind is "Day Of Birth" then
  433.         return findInList(dayOfBirthHeaders, fileContents)
  434.     end if
  435.    
  436.     --BEGIN FIND BIRTH YEAR                                                                                         BEGIN FIND BIRTH YEAR
  437.     if columnToFind is "Year Of Birth" then
  438.         return findInList(yearOfBirthHeaders, fileContents)
  439.     end if
  440.    
  441.     --BEGIN FIND LAST NAME                                                                                      BEGIN FIND LAST NAME
  442.     if columnToFind is "First Name" then
  443.         return findInList(firstNameHeaders, fileContents)
  444.     end if
  445.    
  446.     --BEGIN FIND LAST NAME                                                                                      BEGIN FIND LAST NAME
  447.     if columnToFind is "Last Name" then
  448.         return findInList(lastNameHeaders, fileContents)
  449.     end if
  450.    
  451.     --BEGIN FIND ADDRESS STREET                                                                             BEGIN FIND ADDRESS STREET
  452.     if columnToFind is "Address Street" then
  453.         return findInList(addressStreetHeaders, fileContents)
  454.     end if
  455.    
  456.     --BEGIN FIND ADDRESS CITY                                                                                   BEGIN FIND ADDRESS CITY
  457.     if columnToFind is "Address City" then
  458.         return findInList(addressCityHeaders, fileContents)
  459.     end if
  460.    
  461.     --BEGIN FIND ADDRESS STATE                                                                                  BEGIN FIND ADDRESS STATE
  462.     if columnToFind is "Address State" then
  463.         return findInList(addressStateHeaders, fileContents)
  464.     end if
  465.    
  466.     --BEGIN FIND ADDRESS ZIP                                                                                    BEGIN FIND ADDRESS ZIP
  467.     if columnToFind is "Address Zip" then
  468.         return findInList(addressZipHeaders, fileContents)
  469.     end if
  470.    
  471.     --BEGIN FIND PHONE AREA CODE                                                                                BEGIN FIND PHONE AREA CODE
  472.     if columnToFind is "Phone Area Code" then
  473.         return findInList(phoneAreaCodeHeaders, fileContents)
  474.     end if
  475.    
  476.     --BEGIN FIND PHONE NUMBER                                                                                   BEGIN FIND PHONE NUMBER
  477.     if columnToFind is "Phone Number" then
  478.         return findInList(phoneNumberHeaders, fileContents)
  479.     end if
  480.    
  481.     --BEGIN FIND RESCUE EMAIL                                                                                   BEGIN FIND RESCUE EMAIL
  482.     if columnToFind is "Rescue Email (Optional)" then
  483.         return findInList(rescueEmailHeaders, fileContents)
  484.     end if
  485.    
  486.     --BEGIN FIND ACCOUNT STATUS                                                                             BEGIN FIND ACCOUNT STATUS
  487.     if columnToFind is "Account Status" then
  488.         return findInList(accountStatusHeaders, fileContents)
  489.     end if
  490.    
  491. end findColumn
  492.  
  493. -----------------------------------------
  494.  
  495. on findInList(matchList, listContents)
  496.     try
  497.         set findState to "Not Found"
  498.         set findLocation to 0
  499.         repeat with columnItemLoopCounter from 1 to (count of items of (item 1 of listContents))
  500.             repeat with testForMatchLoopCounter from 1 to (count of matchList)
  501.                 if item columnItemLoopCounter of (item 1 of listContents) is item testForMatchLoopCounter of matchList then
  502.                     set findState to "Found"
  503.                     set findLocation to columnItemLoopCounter
  504.                     exit repeat
  505.                 end if
  506.             end repeat
  507.             if findState is "Found" then exit repeat
  508.         end repeat
  509.         return {findState, findLocation} as list
  510.     on error
  511.         display dialog "Hmm Well, I was looking for something in the file, and something went wrong." buttons "Bummer"
  512.         return 0
  513.     end try
  514. end findInList
  515.  
  516. -----------------------------------------
  517.  
  518. --BEGIN GET COLUMN CONTENTS                                                                                             BEGIN GET COLUMN CONTENTS
  519. on getColumnContents(columnToGet, fileContents)
  520.     set columnContents to {}
  521.     repeat with loopCounter from 1 to (count of items of fileContents)
  522.         set columnContents to columnContents & 1
  523.         set item loopCounter of columnContents to item columnToGet of item loopCounter of fileContents
  524.     end repeat
  525.     return columnContents
  526. end getColumnContents
  527.  
  528. -----------------------------------------
  529.  
  530. on ReadCsvFile(chosenFile)
  531.     --Check to see if we are being passed a method instead of a file to open
  532.     set method to ""
  533.     try
  534.         if chosenFile is "Choose" then
  535.             set method to "Choose"
  536.         end if
  537.     end try
  538.    
  539.     try
  540.         if method is "Choose" then
  541.             set chosenFile to choose file
  542.         end if
  543.        
  544.         set fileOpened to (characters 1 thru -((count every item of (name extension of (info for chosenFile))) + 2) of (name of (info for chosenFile))) as string
  545.         set testResult to TestCsvFile(chosenFile)
  546.        
  547.         if testResult is yes then
  548.             set openFile to open for access chosenFile
  549.             set fileContents to read chosenFile
  550.             close access openFile
  551.             return fileContents
  552.         end if
  553.        
  554.     on error
  555.         close access openFile
  556.         display dialog "Something bjorked when oppening the file!" buttons "Well bummer"
  557.         return {}
  558.     end try
  559. end ReadCsvFile
  560.  
  561. -----------------------------------------
  562.  
  563. on TestCsvFile(chosenFile)
  564.     set chosenFileKind to type identifier of (info for chosenFile)
  565.     if chosenFileKind is "CSV Document" then
  566.         return yes
  567.     else
  568.         if chosenFileKind is "public.comma-separated-values-text" then
  569.             return yes
  570.         else
  571.             display dialog "Silly " & (word 1 of the long user name of (system info)) & ", that file is not a .CSV!" buttons "Oops, my bad"
  572.             return no
  573.         end if
  574.     end if
  575. end TestCsvFile
  576.  
  577. -----------------------------------------
  578.  
  579. on ParseCsvFile(fileContents)
  580.     try
  581.         set parsedFileContents to {} --Instantiate our list to hold parsed file contents
  582.         set delimitersOnCall to AppleScript's text item delimiters --Copy the delimiters that are in place when this handler was called
  583.         set AppleScript's text item delimiters to "," --Set delimiter to commas
  584.        
  585.         --Parse each line (paragraph) from the unparsed file contents
  586.         set lineCount to (count of paragraphs in fileContents)
  587.         repeat with loopCounter from 1 to lineCount --Loop through each line in the file, one at a time
  588.             set parsedFileContents to parsedFileContents & 1 --Add a new item to store the parsed paragraph
  589.             set item loopCounter of parsedFileContents to (every text item of paragraph loopCounter of fileContents) --Parse a line from the file into individual items and store them in the item created above
  590.         end repeat
  591.        
  592.         set AppleScript's text item delimiters to delimitersOnCall --Set Applescript's delimiters back to whatever they were when this handler was called
  593.         return parsedFileContents --Return our fancy parsed contents
  594.     on error
  595.         display dialog "Woah! Um, that's not supposed to happen." & return & return & "Something goofed up bad when I tried to read the file!" buttons "Ok, I'll take a look at the file"
  596.         return fileContents
  597.     end try
  598. end ParseCsvFile
  599.  
  600. -----------------------------------------
  601.  
  602. on verifyPage(expectedElementString, expectedElementLocation, expectedElementCount, verificationTimeout, requiresGroup)
  603.     tell application "System Events"
  604.         --
  605.         repeat until description of scroll area 1 of window 1 of application process "iTunes" is "Apple logo"
  606.             delay (masterDelay * processDelay)
  607.         end repeat
  608.        
  609.         my GetItunesStatusUntillLcd("Does Not Match", itunesAccessingString, 4, "times. Check for:", (verificationTimeout * (1 / checkFrequency)), "intervals of", checkFrequency, "seconds")
  610.         (*repeat
  611.             set lcdStatus to my GetItunesStatusUntillLcd("Does Not Match", itunesAccessingString, 4, "times. Check for:", (verificationTimeout * (1 / checkFrequency)), "intervals of", checkFrequency, "seconds")
  612.             if lcdStatus is "Matched" then exit repeat
  613.             delay masterDelay
  614.         end repeat*)
  615.        
  616.         set elementCount to count every UI element of UI element 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 of application process "iTunes"
  617.        
  618.         repeat with timeoutLoopCounter from 1 to verificationTimeout --Loop will be ended before reaching verificationTimeout if the expectedElementString is successfully located
  619.             if timeoutLoopCounter is equal to verificationTimeout then return "unverified"
  620.            
  621.             if expectedElementCount is 0 then set expectedElementCount to elementCount --Use 0 to disable element count verification
  622.            
  623.             if elementCount is equal to expectedElementCount then
  624.                 set everyTitle to {}
  625.                
  626.                 if requiresGroup then
  627.                     set elementToTest to UI element expectedElementLocation of group 1 of UI element 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 of application process "iTunes"
  628.                 else
  629.                     set elementToTest to UI element expectedElementLocation of UI element 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 of application process "iTunes"
  630.                 end if
  631.                
  632.                 set elementProperties to properties of elementToTest
  633.                
  634.                 try
  635.                     set elementString to title of elementProperties
  636.                     --set elementString to (text items 1 through (count of text items in expectedElementString) of elementString) as string
  637.                 end try
  638.                 if elementString is equal to expectedElementString then
  639.                     return "verified"
  640.                 end if
  641.             end if
  642.             delay 1
  643.         end repeat
  644.     end tell
  645. end verifyPage
  646.  
  647. -----------------------------------------
  648.  
  649. on CheckForErrors()
  650.     if scriptAction is "Continue" then --This is to make sure a previous abort hasn't already been thrown.
  651.         if errorList is not {} then --If there are errors in the list
  652.            
  653.             set errorAction to button returned of (display dialog "Errors were detected. What would you like to do?" buttons {"Abort", "Skip User", "Review"} default button "Review") as string
  654.            
  655.             if errorAction is "Abort" then
  656.                 set scriptAction to "Abort" --This sets the global abort action
  657.                 return "Abort" --This breaks out of the remainder of the error checker
  658.             end if
  659.            
  660.             if errorAction is "Review" then
  661.                 repeat with loopCounter from 1 to (count of items in errorList) --Cycle through all the errors in the list
  662.                     if errorAction is "Abort" then
  663.                         set scriptAction to "Abort" --This sets the global abort action
  664.                         return "Abort" --This breaks out of the remainder of the error checker
  665.                     else
  666.                         set errorAction to button returned of (display dialog "Showing error " & loopCounter & " of " & (count of items in errorList) & ":" & return & return & item loopCounter of errorList & return & return & "What would you like to do?" buttons {"Abort", "Manually Correct"} default button "Manually Correct") as string
  667.                         if errorAction is "Manually Correct" then set errorAction to button returned of (display dialog "Click continue when the error has been corrected." & return & "If you cannot correct the error, then you may skip this user or abort the entire script" buttons {"Abort", "Skip User", "Continue"} default button "Continue") as string
  668.                     end if
  669.                 end repeat
  670.                 set errorList to {} --Clear errors if we've made it all the way through the loops
  671.                 set scriptAction to errorAction
  672.             end if
  673.            
  674.         end if --for error check
  675.     end if --for abort check
  676. end CheckForErrors
  677.  
  678. -----------------------------------------
  679.  
  680. on SignOutItunesAccount()
  681.     if scriptAction is "Continue" then --This is to make sure an abort hasn't been thrown
  682.         tell application "System Events"
  683.             --Tell iTunes to open iBooks. Still submits information to Apple but moves the script along much faster
  684.             tell application "iTunes" to open location ibooksLinkLocation
  685.             delay masterDelay
  686.            
  687.             repeat until description of scroll area 1 of window 1 of application process "iTunes" is "Apple logo"
  688.                 delay (masterDelay * processDelay)
  689.             end repeat
  690.            
  691.             set storeMenu to menu "Store" of menu bar item "Store" of menu bar 1 of application process "iTunes"
  692.             set storeMenuItems to title of every menu item of storeMenu
  693.         end tell
  694.        
  695.         repeat with loopCounter from 1 to (count of items in storeMenuItems)
  696.             if item loopCounter of storeMenuItems is "Sign Out" then
  697.                 tell application "System Events"
  698.                     click menu item "Sign Out" of storeMenu
  699.                 end tell
  700.             end if
  701.         end repeat
  702.     end if
  703. end SignOutItunesAccount
  704.  
  705. -----------------------------------------
  706.  
  707. on GetItunesStatusUntillLcd(matchType, stringToMatch, matchDuration, "times. Check for:", checkDuration, "intervals of", checkFrequency, "seconds")
  708.     set loopCounter to 0
  709.     set matchedFor to 0
  710.     set itunesLcdText to {}
  711.    
  712.     repeat
  713.         set loopCounter to loopCounter + 1
  714.        
  715.         if loopCounter is greater than or equal to (checkDuration * checkFrequency) then
  716.             return "Unmatched"
  717.         end if
  718.        
  719.         set itunesLcdText to itunesLcdText & ""
  720.         tell application "System Events"
  721.             try
  722.                 --set item loopCounter of itunesLcdText to value of static text 1 of scroll area 1 of window 1 of application process "iTunes"
  723.                 set item loopCounter of itunesLcdText to value of static text 1 of scroll area 1 of window 1 of application process "iTunes"
  724.             end try
  725.         end tell
  726.        
  727.         if matchType is "Matches" then
  728.             if item loopCounter of itunesLcdText is stringToMatch then
  729.                 set matchedFor to matchedFor + 1
  730.             else
  731.                 set matchedFor to 0
  732.             end if
  733.         end if
  734.        
  735.         if matchType is "Does Not Match" then
  736.             if item loopCounter of itunesLcdText is not stringToMatch then
  737.                 set matchedFor to matchedFor + 1
  738.             else
  739.                 set matchedFor to 0
  740.             end if
  741.         end if
  742.        
  743.         if matchedFor is greater than or equal to matchDuration then
  744.             return "Matched"
  745.         end if
  746.         delay checkFrequency
  747.     end repeat
  748.    
  749. end GetItunesStatusUntillLcd
  750.  
  751. -----------------------------------------
  752.  
  753. on installIbooks()
  754.     delay (masterDelay * processDelay)
  755.     if scriptAction is "Continue" then --This is to make sure an abort hasn't been thrown
  756.        
  757.         -- AF 2012-05-14 Open location instead of .inetloc
  758.         tell application "iTunes" to open location ibooksLinkLocation
  759.         delay (masterDelay * processDelay)
  760.         --set pageVerification to verifyPage("iBooks", "iBooks", 42, netDelay, true) --Looking for "iBooks", in the second element, on a page with a count of 39 elements, with a timeout of 5, and it requires the use of "group 1" for checking
  761.        
  762.         --if pageVerification is "verified" then --Actually click the button to obtain iBooks
  763.         delay (masterDelay * processDelay)
  764.         tell application "System Events"
  765.             try
  766.                 set freeButton to button 1 of group 2 of UI element 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "iTunes" of application process "iTunes"
  767.                 if description of freeButton is "$0.00 Free, iBooks" then
  768.                     click freeButton
  769.                 else
  770.                     set errorList to errorList & "Unable to locate install app button by its description."
  771.                 end if
  772.             on error
  773.                 set errorList to errorList & "Unable to locate install app button by its description."
  774.             end try
  775.         end tell
  776.         set pageVerification to ""
  777.     else --Throw error if page didn't verify
  778.         set errorList to errorList & "Unable to verify that iTunes is open at the iBooks App Store Page."
  779.         --end if
  780.        
  781.     end if
  782. end installIbooks
  783.  
  784. -----------------------------------------
  785.  
  786. on ClickCreateAppleIDButton()
  787.     delay (masterDelay * processDelay)
  788.     if scriptAction is "Continue" then --This is to make sure an abort hasn't been thrown
  789.         --Verification text for window:
  790.         --get value of static text 1 of window 1 of application process "iTunes" --should be equal to "Sign In to the iTunes Store"
  791.         tell application "System Events"
  792.             if value of static text 1 of window 1 of application process "iTunes" is "Sign In to the iTunes Store" then
  793.                 try
  794.                     click button "Create Apple ID" of window 1 of application process "iTunes"
  795.                 on error
  796.                     set errorList to errorList & "Unable to locate and click button ''Create Apple ID'' on ID sign-in window"
  797.                 end try
  798.             else
  799.                 set errorList to errorList & "Unable to locate sign-in window and click ''Create Apple ID''"
  800.             end if
  801.         end tell
  802.     end if
  803. end ClickCreateAppleIDButton
  804.  
  805. -----------------------------------------
  806.  
  807. on ClickContinueOnPageOne()
  808.     delay (masterDelay * processDelay)
  809.     set pageVerification to verifyPage("Welcome to the iTunes Store", "Welcome to the iTunes Store", 12, netDelay, false) ----------Verify we are at page 1 of the Apple ID creation page
  810.     if pageVerification is "verified" then
  811.        
  812.         try
  813.             tell application "System Events"
  814.                 set contButton to button "Continue" of UI element 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 of application process "iTunes"
  815.                 if title of contButton is "Continue" then
  816.                     click contButton
  817.                 else
  818.                     set errorList to errorList & "Unable to locate and click the Continue button on page ''Welcome to iTunes Store''."
  819.                 end if
  820.             end tell
  821.         on error
  822.             set errorList to errorList & "Unable to locate and click the Continue button on page ''Welcome to iTunes Store''."
  823.         end try
  824.        
  825.         set pageVerification to ""
  826.     else
  827.         set errorList to errorList & "Unable to verify that iTunes is open at the first page of the Apple ID creation process."
  828.     end if
  829. end ClickContinueOnPageOne
  830.  
  831. -----------------------------------------
  832.  
  833. on AgreeToTerms()
  834.     delay (masterDelay * processDelay)
  835.     set pageVerification to verifyPage("Terms and Conditions and Apple Privacy Policy", "Terms and Conditions and Apple Privacy Policy", 16, netDelay, false) ----------Verify we are at page 1 of the Apple ID creation page
  836.     if pageVerification is "verified" then
  837.         tell application "System Events"
  838.            
  839.             --Check box
  840.             try
  841.                 set agreeCheckbox to checkbox "I have read and agree to these terms and conditions." of group 4 of UI element 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 of application process "iTunes"
  842.                 set buttonVerification to title of agreeCheckbox
  843.                 if buttonVerification is "I have read and agree to these terms and conditions." then
  844.                     click agreeCheckbox
  845.                 else
  846.                     set errorList to errorList & "Unable to locate and check box ''I have read and agree to these terms and conditions.''"
  847.                 end if
  848.             on error
  849.                 set errorList to errorList & "Unable to locate and check box ''I have read and agree to these terms and conditions.''"
  850.             end try
  851.            
  852.             --delay (masterDelay * processDelay) --We need to pause a second for System Events to realize we have checked the box
  853.             delay 1
  854.             my CheckForErrors()
  855.            
  856.            
  857.             if scriptAction is "Continue" then
  858.                 try
  859.                     set agreeButton to button "Agree" of UI element 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 of application process "iTunes"
  860.                     set buttonVerification to title of agreeButton
  861.                     if buttonVerification is "Agree" then
  862.                         click agreeButton
  863.                     else
  864.                         set errorList to errorList & "Unable to locate and click button ''Agree''."
  865.                     end if
  866.                 on error
  867.                     set errorList to errorList & "Unable to locate and click button ''Agree''."
  868.                 end try
  869.             else
  870.                 set errorList to errorList & "Unable to locate and click button ''Agree''."
  871.             end if
  872.            
  873.         end tell
  874.     end if
  875.    
  876. end AgreeToTerms
  877.  
  878. -----------------------------------------
  879. on theForm()
  880.     tell application "System Events"
  881.         set theForm to UI element 1 of scroll area 3 of window 1 of application process "iTunes"
  882.         return theForm
  883.     end tell
  884. end theForm
  885.  
  886. -----------------------------------------
  887.  
  888. on FillInField(fieldName, theField, theValue)
  889.     tell application "System Events"
  890.         try
  891.             set focused of theField to true
  892.             set value of theField to theValue
  893.             --if value of theField is not theValue then
  894.             --  set errorList to errorList & ("Unable to fill " & fieldName & " to '" & theValue & "'.")
  895.             -- end if
  896.             --on error
  897.             --  set errorList to errorList & ("Unable to fill " & fieldName & ". ")
  898.         end try
  899.     end tell
  900. end FillInField
  901.  
  902. on FillInKeystroke(fieldName, theField, theValue)
  903.     tell application "System Events"
  904.         set frontmost of application process "iTunes" to true --Verify that iTunes is the front window before performing keystroke event
  905.         try
  906.             set focused of theField to true
  907.             keystroke theValue
  908.         on error
  909.             set errorList to errorList & ("Unable to fill " & fieldName & ". ")
  910.         end try
  911.     end tell
  912. end FillInKeystroke
  913.  
  914. on FillInPopup(fieldName, theField, theValue, maximum)
  915.     tell application "System Events"
  916.         set frontmost of application process "iTunes" to true --Verify that iTunes is the front window before performing keystroke event
  917.         try
  918.             -- iTunes doesn't allow direct access to popup menus. So we step through instead.
  919.             repeat with loopCounter from 1 to maximum
  920.                 if value of theField is theValue then exit repeat
  921.                
  922.                 set focused of theField to true
  923.                 delay 0.1
  924.                 keystroke " " -- Space to open the menu
  925.                 keystroke (key code 125) -- down arrow
  926.                 keystroke " " -- Space to close the menu
  927.             end repeat
  928.            
  929.             if value of theField is not theValue then set errorList to errorList & {"Unable to fill " & fieldName, theField & ". "}
  930.         on error
  931.             set errorList to errorList & ("Unable to fill " & fieldName & ". ")
  932.         end try
  933.     end tell
  934. end FillInPopup
  935.  
  936. on ClickThis(fieldName, theField)
  937.     tell application "System Events"
  938.         try
  939.             click theField
  940.         on error
  941.             set errorList to errorList & ("Unable to click " & fieldName & ". ")
  942.         end try
  943.     end tell
  944. end ClickThis
  945.  
  946. -----------------------------------------
  947.  
  948. on ProvideAppleIdDetails(appleIdEmail, appleIdPassword, appleIdSecretQuestion1, appleIdSecretAnswer1, appleIdSecretQuestion2, appleIdSecretAnswer2, appleIdSecretQuestion3, appleIdSecretAnswer3, rescueEmail, userBirthMonth, userBirthDay, userBirthYear)
  949.     if scriptAction is "Continue" then --This is to make sure an abort hasn't been thrown
  950.         set pageVerification to verifyPage("Provide Apple ID Details", "Provide Apple ID Details", 0, (netDelay * processDelay), false)
  951.         if pageVerification is "Verified" then
  952.             tell application "System Events"
  953.                 set theForm to UI element 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 of application process "iTunes"
  954.                 -----------
  955.                 tell me to FillInField("Email", text field "email@example.com" of group 2 of theForm, appleIdEmail)
  956.                 -----------
  957.                 tell me to FillInKeystroke("Password", text field "Password" of group 2 of group 3 of theForm, appleIdPassword)
  958.                 -----------
  959.                 tell me to FillInKeystroke("Verify Password", text field "Retype your password" of group 4 of group 3 of theForm, appleIdPassword)
  960.                 -----------
  961.                 tell me to FillInPopup("First Security Question", pop up button 1 of group 1 of group 6 of theForm, appleIdSecretQuestion1, 5)
  962.                 tell me to FillInField("First Answer", text field 1 of group 2 of group 6 of theForm, appleIdSecretAnswer1)
  963.                 -----------
  964.                 tell me to FillInPopup("Second Security Question", pop up button 1 of group 1 of group 7 of theForm, appleIdSecretQuestion2, 5)
  965.                 tell me to FillInField("Second Answer", text field 1 of group 2 of group 7 of theForm, appleIdSecretAnswer2)
  966.                 -----------
  967.                 tell me to FillInPopup("Third Security Question", pop up button 1 of group 1 of group 8 of theForm, appleIdSecretQuestion3, 6)
  968.                 tell me to FillInField("Third Answer", text field 1 of group 2 of group 8 of theForm, appleIdSecretAnswer3)
  969.                 -----------
  970.                 tell me to FillInField("Optional Rescue Email", text field "rescue@example.com" of group 11 of theForm, rescueEmail)
  971.                 -----------
  972.                 tell me to FillInPopup("Month", pop up button 1 of group 1 of group 13 of theForm, userBirthMonth, 12)
  973.                 tell me to FillInPopup("Day", pop up button 1 of group 2 of group 13 of theForm, userBirthDay, 31)
  974.                 tell me to FillInField("Year", text field "Year" of group 3 of group 13 of theForm, userBirthYear)
  975.                 -----------
  976.                 set releaseCheckbox to checkbox "New releases and additions to the iTunes Store." of group 15 of theForm
  977.                 set newsCheckbox to checkbox "News, special offers, and information about related products and services from Apple." of group 16 of theForm
  978.                 if value of releaseCheckbox is 1 then
  979.                     tell me to ClickThis("New releases and additions to the iTunes Store.", releaseCheckbox)
  980.                 end if
  981.                 if value of newsCheckbox is 1 then
  982.                     tell me to ClickThis("News, special offers, and information about related products and services from Apple.", newsCheckbox)
  983.                 end if
  984.                 -----------
  985.                
  986.                 my CheckForErrors() --Check for errors before continuing to the next page
  987.                
  988.                 if dryRun is true then
  989.                     set dryRunSucess to button returned of (display dialog "Did everything fill in properly?" buttons {"Yes", "No"}) as text
  990.                     if dryRunSucess is "No" then
  991.                         set scriptAction to button returned of (display dialog "What would you like to do?" buttons {"Abort", "Continue"}) as text
  992.                     end if
  993.                 end if
  994.                
  995.                 if scriptAction is "Continue" then
  996.                     tell me to click button "Continue" of theForm
  997.                 end if
  998.             end tell
  999.         else --(If page didn't verify)
  1000.             set errorList to errorList & "Unable to verify that the ''Provide Apple ID Details'' page is open and fill its contents."
  1001.         end if
  1002.     end if
  1003. end ProvideAppleIdDetails
  1004.  
  1005. on ProvidePaymentDetails(userFirstName, userLastName, addressStreet, addressCity, addressState, addressZip, phoneAreaCode, phoneNumber)
  1006.     if scriptAction is "Continue" then --This is to make sure an abort hasn't been thrown
  1007.         set pageVerification to verifyPage("Provide a Payment Method", "Provide a Payment Method", 0, (netDelay * processDelay), false)
  1008.        
  1009.         if pageVerification is "Verified" then
  1010.             tell application "System Events"
  1011.                 click radio button "None" of radio group 1 of theForm
  1012.             end tell
  1013.         end if
  1014.        
  1015.         --Wait for the page to change after selecting payment type
  1016.         set checkFrequency to 0.25 --How often (in seconds) the iTunes LCD will be checked to see if iTunes is busy loading the page
  1017.        
  1018.         repeat
  1019.             set lcdStatus to GetItunesStatusUntillLcd("Does Not Match", itunesAccessingString, 4, "times. Check for:", (netDelay * (1 / checkFrequency)), "intervals of", checkFrequency, "seconds")
  1020.             if lcdStatus is "Matched" then exit repeat
  1021.             delay masterDelay
  1022.         end repeat
  1023.        
  1024.         tell application "System Events"
  1025.             try
  1026.                 set frontmost of application process "iTunes" to true --Verify that iTunes is the front window before performing keystroke event
  1027.                 set focused of pop up button 1 of group 1 of group 7 of theForm to true
  1028.                 keystroke "Mr"
  1029.             on error
  1030.                 set errorList to errorList & "Unable to set ''Title' to 'Mr.'"
  1031.             end try
  1032.             -----------
  1033.             try
  1034.                 set value of text field "First name" of group 1 of group 8 of theForm to userFirstName
  1035.             on error
  1036.                 set errorList to errorList & "Unable to set ''First Name'' field to " & userFirstName
  1037.             end try
  1038.             -----------
  1039.             try
  1040.                 set value of text field "Last name" of group 2 of group 8 of theForm to userLastName
  1041.             on error
  1042.                 set errorList to errorList & "Unable to set ''Last Name'' field to " & userLastName
  1043.             end try
  1044.             -----------
  1045.             try
  1046.                 set value of text field "Street" of group 1 of group 9 of theForm to addressStreet
  1047.             on error
  1048.                 set errorList to errorList & "Unable to set ''Street Address'' field to " & addressStreet
  1049.             end try
  1050.             -----------
  1051.             try
  1052.                 set value of text field "City" of group 1 of group 10 of theForm to addressCity
  1053.             on error
  1054.                 set errorList to errorList & "Unable to set ''City'' field to " & addressCity
  1055.             end try
  1056.             -----------
  1057.             try
  1058.                 set frontmost of application process "iTunes" to true --Verify that iTunes is the front window before performking keystroke event
  1059.                 set focused of pop up button 1 of group 2 of group 10 of theForm to true
  1060.                 keystroke addressState
  1061.             on error
  1062.                 set errorList to errorList & "Unable to set ''State'' drop-down to " & addressState
  1063.             end try
  1064.             -----------
  1065.             try
  1066.                 set value of text field "Zip" of group 3 of group 10 of theForm to addressZip
  1067.             on error
  1068.                 set errorList to errorList & "Unable to set ''Zip'' field to " & addressZip
  1069.             end try
  1070.             -----------
  1071.             try
  1072.                 set value of text field "Area code" of group 1 of group 11 of theForm to phoneAreaCode
  1073.             on error
  1074.                 set errorList to errorList & "Unable to set ''Area Code'' field to " & phoneAreaCode
  1075.             end try
  1076.             -----------
  1077.             try
  1078.                 set value of text field "Phone" of group 2 of group 11 of theForm to phoneNumber
  1079.             on error
  1080.                 set errorList to errorList & "Unable to set ''Phone Number'' field to " & phoneNumber
  1081.             end try
  1082.             -----------
  1083.            
  1084.             my CheckForErrors()
  1085.            
  1086.             if dryRun is true then --Pause to make sure all the fields filled properly
  1087.                 set dryRunSucess to button returned of (display dialog "Did everything fill in properly?" buttons {"Yes", "No"}) as text
  1088.                 if dryRunSucess is "No" then
  1089.                     set scriptAction to button returned of (display dialog "What would you like to do?" buttons {"Abort", "Continue"}) as text
  1090.                 end if
  1091.             end if
  1092.            
  1093.             if dryRun is false then --Click the "Create Apple ID" button as long as we aren't in "Dry Run" mode
  1094.                 if scriptAction is "Continue" then --Continue as long as no errors occurred
  1095.                     try
  1096.                         click button "Create Apple ID" of theForm
  1097.                         delay 10
  1098.                     on error
  1099.                         set errorList to errorList & "Unable to click ''Create Apple ID'' button."
  1100.                     end try
  1101.                 end if --End "Continue if no errors" statement
  1102.             else --If we are doing a dry run then...
  1103.                 set dryRunChoice to button returned of (display dialog "Completed. Would you like to stop the script now, continue ''dry running'' with the next user in the CSV (if applicable), or run the script ''for real'' starting with the first user?" buttons {"Stop Script", "Continue Dry Run", "Run ''For Real''"}) as text
  1104.                 if dryRunChoice is "Stop Script" then set scriptAction to "Stop"
  1105.                 if dryRunChoice is "Run ''For Real''" then
  1106.                     set currentUserNumber to 0
  1107.                     set dryRun to false
  1108.                 end if
  1109.             end if --End "dry Run" if statement
  1110.            
  1111.         end tell --End "System Events" tell
  1112.     end if --End main error check IF
  1113. end ProvidePaymentDetails
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement