(*The Idea here is that you feed the script a tab-delimited file that contains two fields: a username, i.e. "jwelch" and a long name, i.e. "John Welch". I like to use tabs because that way, name punctuation doesn't trip things up. I've not seen too many names with tabs in them. The order is important, so a sample line would be:
jwelch John Welch.
No quotes, no spaces between fields nothing just username TAB John Welch. Since the script is looking for the actual tab character, inserting 5 spaces won't work. You can of course, add more tab fields in, you just have to account for them in the code*)
set theSourceFile to choose file --pick the tab delimited file
set theFileReference to open for access theSourceFile without write permission --open for reading only.
--the "without write permission" part is just a safety measure.
--it returns a file reference that allows you do do other things with that file in AppleScript
set theNames to read theFileReference --Like read its contents
close access theFileReference --we always close our handles, it's good practice.
set theDesktop to ((POSIX path of home directory of (system info)) & "Desktop/") --creates a Unix path to the desktop folder of
--the user running the script. This is done through Standard Additions, so no tell block is needed. If you can use
--Standard Additions for things instead of an application tell block, you should, it's faster and less resource-intensive.
set saveTID to AppleScript's text item delimiters --Text item delimiters are how AppleScript defines the difference between text
--and "not text". Sort of. Anyway, normally, the delimiters, or "TIDs" for the AppleScript cognescenti, are "", the null
--character. In this case, we're going to save that value to "saveTID" and change the delimiters.
set AppleScript's text item delimiters to " " --Set the new delimter to be the "tab" character. What you type is "\t". Once you
--compile, you get the actual tab, hence quotes around "nothing".
set theNameList to every paragraph of theNames --this turns every paragraph in the file, (defined as a string ending in the
--return character) to an item in an AppleScript list. We'll see why this is handy.
tell application "iPhone Configuration Utility"
repeat with x in theNameList --Here's why a list is handy. It gives us a built-in loop counter. This line sets 'x' to
--whatever item in the list we're currently working on.
set theItemList to every text item of x --remember those TIDs we set? Well now, a text item is a bunch of text that
--isn't a tab. So jwelch\tJohn Welch becomes a list of two items: "jwelch" and "John Welch". Since AppleScript
--is now only delimiting based on tabs, no other character will be a delimiter for these kinds of operations.
--TIDS can be very powerful.
set theUserName to item 1 of theItemList --this is the short username
set theLongName to item 2 of theItemList --Long name, or "human name"
set theEmailAddress to theUserName & "@bynkii.com" --build the email address
set theFileName to theUserName & ".mobileconfig" --build the file name
set theExportPath to theDesktop & theFileName --set the export path to the file name on the current user's desktop
set theProfileName to theLongName & "'s scripted profile" --This creates a better name for the profile in the
--iPhone Configuration Utility.
set theProfile to make new configuration profile with properties {displayed name:theProfileName, profile identifier:"com.bynkii.scriptedprofile", organization:"Mr. Wonderful", account description:"I made it in a SCRIPT"} --fairly self-evident, I explain this in the book in some detail. You should get the book, it's really awesome.
tell theProfile --by "telling" the profile, we simplify the overall code a LOT.
make new restrictions payload with properties {allow adding game center friends:false, allow multiplayer gaming:false, explicit content allowed:false, YouTube allowed:false} --Here's the restrictions
make new email payload with properties {account description:"scripted email account", account name:theLongName, account protocol:IMAP, email address:theEmailAddress, incoming server hostname:"imap.bynkii.com", incoming server port:143, incoming server username:theUserName, incoming server uses password authentication:true, incoming server uses SSL:false, outgoing server hostname:"smtp.bynkii.com", outgoing server port:587, outgoing server username:theUserName, outgoing server uses password authentication:true, outgoing server uses SSL:false, use incoming password when sending mail:true} --this builds both the IMAP and SMTP accounts. Note the use of
--theLongName, theUserName, theEmailAddress, etc.
make new CalDAV payload with properties {account description:"scripted CalDAV account", hostname:"calendar.bynkii.com", port:80, principal URL:"http://calendar.bynkii.com/caldav", use SSL:false, user name:theUserName} --let's create a
--caldav account. Ironically, you can't script this for iCal on Mac OS X, but you can for the iPhone. Woot?
end tell --we've told that profile all it needs to know
export theProfile to theExportPath --tell the iPCU to export the file to the export path. The iPhone Configuration
--Utility requires Unix-style paths, which is why we made theExportPath one earlier.
end repeat --close out the loop
end tell --end the tell block for the iPCU
set AppleScript's text item delimiters to saveTID --set the TID back to the normal value. SOmewhat unneccessary, but good practice