SHARE
TWEET

Enterprise iOS Apple ID Creator.applescript

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