aboutsummaryrefslogtreecommitdiff
path: root/build/blog/2015-03-22-back-up-os-x-app-icons.html
blob: e243c42f09574cdb27889c21cd429e9660fc8c75 (plain)
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta content="pandoc" name="generator"/>
<meta content="Zhiming Wang" name="author"/>
<meta content="2015-03-22T16:58:50-0700" name="date"/>
<title>Back up OS X app icons</title>
<link href="/img/apple-touch-icon-152.png" rel="apple-touch-icon-precomposed"/>
<meta content="#FFFFFF" name="msapplication-TileColor"/>
<meta content="/img/favicon-144.png" name="msapplication-TileImage"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<link href="/css/normalize.min.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/css/theme.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/css/highlight.css" media="all" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="archival-notice">This blog has been archived.<br/>Visit my home page at <a href="https://zhimingwang.org">zhimingwang.org</a>.</div>
<nav class="nav">
<a class="nav-icon" href="/" title="Home"><!--blog icon--></a>
<a class="nav-title" href="/"><!--blog title--></a>
<a class="nav-author" href="https://github.com/zmwangx" target="_blank"><!--blog author--></a>
</nav>
<article class="content">
<header class="article-header">
<h1 class="article-title">Back up OS X app icons</h1>
<div class="article-metadata">
<time class="article-timestamp" datetime="2015-03-22T16:58:50-0700">March 22, 2015</time>
</div>
</header>
<p>OS X application icons are valuable assets, and it's interesting to see how they evolve over time. This is especially the case when we upgraded to OS X 10.10 Yosemite, when Apple and many design-aware third party developers overhauled (mainly flattened) their icons.</p>
<p>However, we lose all the old icons when we do a major OS upgrade. Technically they still live in Time Machine backups, but those are a pain to pull out. Therefore, I wrote a script just now to back up app icons of all applications living in <code>/Applications</code> (including those symlinked to <code>/Applications</code>, e.g., apps installed through <code>brew cask</code>) and its level-one subdirectories, and <code>/System/Library/CoreServices</code> (for <code>Finder.app</code> and such). Here's the script:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="co">#!/usr/bin/env bash</span>
<span class="kw">function</span><span class="fu"> app_version</span>
<span class="kw">{</span>
    <span class="co"># $1 is the path to the app</span>
    <span class="ex">/usr/libexec/PlistBuddy</span> -c <span class="st">"print CFBundleShortVersionString"</span> <span class="st">"</span><span class="va">$1</span><span class="st">"</span>/Contents/Info.plist <span class="op">2&gt;</span>/dev/null <span class="kw">||</span> <span class="fu">date</span> +%Y%m%d
<span class="kw">}</span>

<span class="kw">function</span><span class="fu"> app_icon_path</span>
<span class="kw">{</span>
    <span class="co"># $1 is the path to the app</span>
    <span class="va">filename=$(</span><span class="ex">/usr/libexec/PlistBuddy</span> -c <span class="st">"print CFBundleIconFile"</span> <span class="st">"</span><span class="va">$1</span><span class="st">"</span>/Contents/Info.plist <span class="op">2&gt;</span>/dev/null<span class="va">)</span>
    [[ <span class="ex">-n</span> <span class="va">${filename}</span> ]] <span class="kw">||</span> <span class="bu">return</span>
    <span class="va">filename=$(</span><span class="fu">basename</span> <span class="st">"</span><span class="va">${filename}</span><span class="st">"</span> .icns<span class="va">)</span>
    <span class="bu">echo</span> <span class="st">"</span><span class="va">$1</span><span class="st">/Contents/Resources/</span><span class="va">${filename}</span><span class="st">.icns"</span>
<span class="kw">}</span>

<span class="kw">function</span><span class="fu"> process_app</span>
<span class="kw">{</span>
    <span class="co"># $1 is the path to the app</span>
    <span class="va">name=$(</span><span class="fu">basename</span> <span class="st">"</span><span class="va">$1</span><span class="st">"</span> .app <span class="kw">|</span> <span class="fu">tr</span> -d <span class="st">' '</span><span class="va">)</span>
    <span class="va">path=$(</span><span class="ex">realpath</span> -e <span class="st">"</span><span class="va">$1</span><span class="st">"</span><span class="va">)</span> <span class="kw">||</span> <span class="kw">{</span> <span class="bu">echo</span> <span class="st">"</span><span class="va">${RED}</span><span class="st">error: broken link '</span><span class="va">${path}</span><span class="st">'</span><span class="va">${RESET}</span><span class="st">"</span> <span class="op">&gt;&amp;2</span><span class="kw">;</span> <span class="bu">return</span> 1<span class="kw">;</span> <span class="kw">}</span>
    <span class="va">version=$(</span><span class="ex">app_version</span> <span class="st">"</span><span class="va">${path}</span><span class="st">"</span><span class="va">)</span>
    <span class="va">icon_path=$(</span><span class="ex">app_icon_path</span> <span class="st">"</span><span class="va">${path}</span><span class="st">"</span><span class="va">)</span>
    [[ <span class="ex">-n</span> <span class="va">${icon_path}</span> ]] <span class="kw">||</span> <span class="kw">{</span> <span class="bu">echo</span> <span class="st">"</span><span class="va">${YELLOW}</span><span class="st">warning: '</span><span class="va">$1</span><span class="st">' has no app icon</span><span class="va">${RESET}</span><span class="st">"</span><span class="kw">;</span> <span class="bu">return</span> 1<span class="kw">;</span> <span class="kw">}</span>
    [[ <span class="ex">-f</span> <span class="va">${icon_path}</span> ]] <span class="kw">||</span> <span class="kw">{</span> <span class="bu">echo</span> <span class="st">"</span><span class="va">${RED}</span><span class="st">error: '</span><span class="va">${icon_path}</span><span class="st">' does not exist</span><span class="va">${RESET}</span><span class="st">"</span> <span class="op">&gt;&amp;2</span><span class="kw">;</span> <span class="bu">return</span> 1<span class="kw">;</span> <span class="kw">}</span>
    <span class="fu">cp</span> <span class="st">"</span><span class="va">${icon_path}</span><span class="st">"</span> <span class="st">"</span><span class="va">${name}</span><span class="st">-</span><span class="va">${version}</span><span class="st">.icns"</span>
    <span class="bu">echo</span> <span class="st">"</span><span class="va">${name}</span><span class="st">-</span><span class="va">${version}</span><span class="st">.icns"</span>
<span class="kw">}</span>

<span class="fu">find</span> /Applications -maxdepth 2 -name <span class="st">'*.app'</span> <span class="kw">|</span> <span class="kw">while</span> <span class="bu">read</span> <span class="va">app</span>; <span class="kw">do</span> <span class="ex">process_app</span> <span class="st">"</span><span class="va">${app}</span><span class="st">"</span><span class="kw">;</span> <span class="kw">done</span>
<span class="fu">find</span> /System/Library/CoreServices -maxdepth 1 -name <span class="st">'*.app'</span> <span class="kw">|</span> <span class="kw">while</span> <span class="bu">read</span> <span class="va">app</span>; <span class="kw">do</span> <span class="ex">process_app</span> <span class="st">"</span><span class="va">${app}</span><span class="st">"</span><span class="kw">;</span> <span class="kw">done</span></code></pre></div>
<p>The script is also available as a <a href="https://gist.github.com/zmwangx/fad97e085045a21ebc1d">gist</a>.</p>
</article>
<hr class="content-separator"/>
<footer class="footer">
<span class="rfooter">
<a class="rss-icon" href="/rss.xml" target="_blank" title="RSS feed"><!--RSS feed icon--></a><a class="atom-icon" href="/atom.xml" target="_blank" title="Atom feed"><!--Atom feed icon--></a><a class="cc-icon" href="https://creativecommons.org/licenses/by/4.0/" target="_blank" title="Released under the Creative Commons Attribution 4.0 International license."><!--CC icon--></a>
<a href="https://github.com/zmwangx" target="_blank">Zhiming Wang</a>
</span>
</footer>
</body>
</html>