u-f_5w
Last Updated: February 25, 2016
·
390
· chrismccoy

Sort your Paid iPhone and iPad Apps

I have been trying to sort paid vs free apps I have bought for my iPhone and iPad, but I found no way to actually sort by this on the devices, or in the actual iTunes App.

Basically Im was attempting to build a database of my Apps on my Devices.

The Only way to do this I found was to goto my purchases in my account in iTunes and click on each individual receipt, and write them down, and since you can't even download the receipt, or copy and paste, it is kind of useless.

That Wouldn't be so bad if I didn't have 27 pages of Apps to go through.

I even called Apple Support and asked them to email me a full list of my apps, they said they did, but I never got it.

So I came up with a way to get a list of all my Apple ID's, and convert that list to actual prices and titles, and even calculate the total amount spent on the App Store.

For this to work, you need all your Apps downloaded in iTunes. So you will need to goto iTunes store and click not in my library, and click on download all.

Step #1

#!/bin/bash

# get list of App Store IDS from iPhone/iPad/iPod touch apps
# note: this will only get the ones you have actually downloaded in your iTunes

for f in ~/Music/iTunes/iTunes\ Media/Mobile\ Applications/*.ipa; do 
unzip -p "$f" "iTunesMetadata.plist" | \
sed -En "/<key>itemId<\\/key>/I,/<integer>/I!d;s/^.*<integer>(.+)<\\/integer>.*$/\\1/Ip"
done

You Would add them to a text file

./getids.sh > ids.txt

Next we need to get the prices and titles matching these ID's, and this tiny little class will help us do that.

Step #2

<?php

class App {
    public $id;                  
    public $price;               
    public $title;               

    function __construct( $id = false, $country = 'US', $language = 'en_us' ) {
        $this->id   = $id;
        $this->json = file_get_contents( 'http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsLookup?country='.$country.'&lang='.$language.'&id='.$id );
        $itunes     = json_decode( $this->json, true );
        $itunes     = $itunes['results'][0];
        $this->price = $itunes['price'];
        $this->title = $itunes['trackCensoredName'];
    }
}

Next we will read the list of ids generated with the bash script.

<?php

include "api.php";

$file = 'ids.txt';

$fh = fopen($file,'r');

while ($line = fgets($fh)) {

  $id = trim($line);
  $app = new App( $id );

  if(!empty($app->title)) { // Apps that were removed have no title

    if($app->price != 0) {
        echo 'ID: ' . $app->id . ' Title: ' . $app->title . ' Price: $' . $app->price . "\n";
    }
  }
}
fclose($fh);
?>

And we want to output the data to a file

php grabprices.php > prices.txt

Now we have a text file with the id, title and price of the apps.

Bonus:

To Calculate the total of the amount spent on the app store, here is a short little one liner.

awk -F '$' '{sum += $2} END {print sum}' paid.txt | sed 's/^/$/; s/$/ Spent on the App Store/; s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t'

Github Link with all the code here, plus list of my ids and prices as sample data