Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created March 22, 2012 17:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsaunders100/2160370 to your computer and use it in GitHub Desktop.
Save rsaunders100/2160370 to your computer and use it in GitHub Desktop.
Re-sign .ipa files and replace the embedded.mobileprovision
-- Erica Sadun, http://ericasadun.com
-- iPhone Developer's Cookbook, 3.0 Edition
-- BSD License, Use at your own risk
-- Adapted by Rob Saunders to also replace embeded profiles
--
--
-- save in Script Editor as Application
-- drag files to its icon in Finder
-- Determine whether the item has a given extension. Return true of false
on checkIsFileType(theFileName, theFileType)
set the_file_name to (name of (info for alias theFileName))
set extension_offset to offset of "." in the_file_name
set file_extension to ""
if (extension_offset > 0) then set file_extension to text (extension_offset + 1) thru (length of the_file_name) of the_file_name
if file_extension is not theFileType then
return false
else
return true
end if
end checkIsFileType
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
on generic_error()
display dialog "ERROR: Please drag a .ipa file (and optionaly a provisioning profile) over the app icon." buttons {"Ok"}
end generic_error
on open file_items
if (count file_items) is not equal to 1 and (count file_items) is not equal to 2 then
generic_error()
else
set ipa_file to null
set prov_file to null
repeat with this_item in file_items
if (checkIsFileType(this_item, "ipa")) then
set ipa_file to this_item
end if
if (checkIsFileType(this_item, "mobileprovision")) then
set prov_file to this_item
end if
end repeat
if (ipa_file is null) then
generic_error()
else
do shell script "rm -f -r ~/Temp1234"
do shell script "rm -f -r /Payload"
set ipa_path to POSIX path of ipa_file
do shell script "mkdir ~/Temp1234"
do shell script "unzip -d ~/Temp1234 " & ipa_path
try
set app_path to do shell script "find ~/Temp1234/Payload -name '*.app'"
if (prov_file is not null) then
set prov_path to POSIX path of prov_file
set new_prov_path to app_path & "/embedded.mobileprovision"
do shell script "rm ~/Temp1234/Payload/*.app/embedded.mobileprovision"
do shell script "rm -f \"" & new_prov_path & "\""
do shell script "cp \"" & prov_path & "\" \"" & new_prov_path & "\""
end if
if (prov_file is not null) then
set other_button to "Dont re-sign"
else
set other_button to "Cancel"
end if
set diag_result to display dialog "Enter then name of the certificate to re-sign with (exactly as it appears on the keychain minus any hex)" buttons {"Ok", other_button} default answer "" default button 1
set which_option to button returned of diag_result
set cert_name to text returned of diag_result
-- Only perform the signing if the user selected Developer or Distribution
if which_option is "Ok" then
try
perform_sign(app_path, cert_name)
on error errmesg number errn
display dialog errmesg
end try
end if
if (which_option is not "Cancel") then
set new_ipa_path to replaceText(".ipa", "_new.ipa", ipa_path)
do shell script "cp -r /Users/robertsaunders/Temp1234/Payload /"
do shell script "zip -r ~/Temp1234/Temp.zip /Payload"
do shell script "cp -fr ~/Temp1234/Temp.zip \"" & new_ipa_path & "\""
end if
do shell script "rm -f -r ~/Temp1234"
do shell script "rm -f -r /Payload"
on error errmesg number errn
display dialog errmesg
do shell script "rm -f -r ~/Temp1234"
do shell script "rm -f -r /Payload"
end try
end if
end if
end open
to perform_sign(this_item, cert_name)
-- retrieve the item path for the .app bundle
set the_path to POSIX path of this_item
-- create the shell command
set unix_command to "/bin/bash -c 'export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate; /usr/bin/codesign -f -s \"" & cert_name & "\" " & the_path & "' 2>&1"
-- retrieve any results of executing the command
set command_result to do shell script unix_command
-- on success, show the successful results (errors are handled in the try block above)
set user_results to "Sucessfully resigned with " & cert_name & " Certificate"
display dialog user_results
end perform_sign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment