-
Notifications
You must be signed in to change notification settings - Fork 0
/
track-down-app-purchases.sh
executable file
·123 lines (105 loc) · 3.14 KB
/
track-down-app-purchases.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
# Track Down App Purchases v0.1.1
# Shell script to get a list of all iOS apps in your iTunes Library,
# showing which Apple ID was used to purchase each app.
#
# Copyright © 2013 Jonathan Hogervorst. All rights reserved.
# This code is licensed under MIT license. See LICENSE for details.
# Source: http://stackoverflow.com/a/6885918/251760
function format_width
{
local WIDTH=$1
local STR=$2
local BYTE_WIDTH=$( echo -n "$STR" | wc -c )
local CHAR_WIDTH=$( echo -n "$STR" | wc -m )
echo $(( $WIDTH + $BYTE_WIDTH - $CHAR_WIDTH ))
}
function print_cell
{
local WIDTH=$1
local STR=$2
local FORMAT_WIDTH=$( format_width "$WIDTH" "$STR" )
printf "%-*.*s" "$FORMAT_WIDTH" "$FORMAT_WIDTH" "$STR"
}
function print_row
{
print_cell 30 $1
printf " | "
print_cell 14 $2
printf " | "
print_cell 30 $3
printf "\n"
}
function plutil_installed
{
hash plutil 2> /dev/null
}
function extract_plist_key
{
local KEY=$1
local PLIST=$2
echo "$PLIST" | sed -e "/<key>$KEY<\\/key>/I,/<string>/I!d" | sed -En "s/^.*<string>(.+)<\\/string>.*$/\\1/Ip"
}
function extract_plist
{
local PLIST_NAME=$1
local ZIP_NAME=$2
local UNZIP_COMMAND="unzip -p \"$ZIP_NAME\" \"$PLIST_NAME\" 2> /dev/null"
if plutil_installed
then
eval "$UNZIP_COMMAND" | plutil -convert xml1 -o - -
else
eval "$UNZIP_COMMAND"
fi
}
echo ""
LIBRARY_PATH_DEFAULT="~/Music/iTunes"
read -p "Path to iTunes Library [$LIBRARY_PATH_DEFAULT]: " LIBRARY_PATH
LIBRARY_PATH="${LIBRARY_PATH:-$LIBRARY_PATH_DEFAULT}"
LIBRARY_PATH="${LIBRARY_PATH/\~/$HOME}"
LIBRARY_PATH="${LIBRARY_PATH%/}"
APP_FILES=$( find "$LIBRARY_PATH/iTunes Media/Mobile Applications" -type f -name "*.ipa" )
INVALID_APP_FILES=""
OIFS="$IFS"
IFS=$'\n'
echo ""
DASHES="------------------------------"
print_row "$DASHES" "$DASHES" "$DASHES"
print_row "App Name" "Bundle Name" "Apple ID"
print_row "$DASHES" "$DASHES" "$DASHES"
for APP_FILE in $APP_FILES
do
APP_PLIST=$( extract_plist "iTunesMetadata.plist" "$APP_FILE" )
APP_APPLE_ID=$( extract_plist_key "appleId" "$APP_PLIST" )
APP_BUNDLE_DISPLAY_NAME=$( extract_plist_key "bundleDisplayName" "$APP_PLIST" )
APP_ITEM_NAME=$( extract_plist_key "itemName" "$APP_PLIST" )
if [ ! -z "$APP_APPLE_ID$APP_BUNDLE_DISPLAY_NAME$APP_ITEM_NAME" ]
then
print_row "$APP_ITEM_NAME" "$APP_BUNDLE_DISPLAY_NAME" "$APP_APPLE_ID"
else
APP_FILE_NAME=$( basename "$APP_FILE" )
INVALID_APP_FILES="• $INVALID_APP_FILES$APP_FILE_NAME\n"
fi
done
print_row "$DASHES" "$DASHES" "$DASHES"
echo ""
if [ ! -z "$INVALID_APP_FILES" ]
then
echo "Some files could not be processed:"
printf "$INVALID_APP_FILES"
echo ""
if ! plutil_installed
then
echo "You could try to install the Xcode Command Line Tools. It includes a utility"
echo "that can convert binary plist files to their XML equivalent. In short, it might"
echo "help this script process more of your apps."
echo ""
echo "Start by installing Xcode from the Mac App Store (for free). Then, go to the"
echo "Downloads tab within the Xcode Preferences menu and click \"Install\" next to the"
echo "Command Line Tools entry."
echo ""
echo "More information and source: https://developer.apple.com/support/xcode/"
echo ""
fi
fi
IFS="$OIFS"