Skip to content

Commit c811487

Browse files
committed
Update app icon once for each build version update.
1 parent b6ee10c commit c811487

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

OpenTerminal.xcodeproj/project.pbxproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
B19FD9FE1FD5A14700A0864A /* FinderWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = B19FD9FD1FD5A14700A0864A /* FinderWindow.swift */; };
2626
B19FDA001FD5A16400A0864A /* FinderFinderWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = B19FD9FF1FD5A16400A0864A /* FinderFinderWindow.swift */; };
2727
B1AED4B028369EA3004B2A19 /* Icon.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1AED4AF28369C94004B2A19 /* Icon.swift */; };
28+
B1AED4B22836B247004B2A19 /* AppInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1AED4B12836B1F2004B2A19 /* AppInfo.swift */; };
2829
B1FF6E1E210948E30056E3C0 /* AppleScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1FF6E1D210948E30056E3C0 /* AppleScript.swift */; };
2930
/* End PBXBuildFile section */
3031

@@ -54,6 +55,7 @@
5455
B19FD9FD1FD5A14700A0864A /* FinderWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FinderWindow.swift; sourceTree = "<group>"; };
5556
B19FD9FF1FD5A16400A0864A /* FinderFinderWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FinderFinderWindow.swift; sourceTree = "<group>"; };
5657
B1AED4AF28369C94004B2A19 /* Icon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Icon.swift; sourceTree = "<group>"; };
58+
B1AED4B12836B1F2004B2A19 /* AppInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppInfo.swift; sourceTree = "<group>"; };
5759
B1FF6E1D210948E30056E3C0 /* AppleScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleScript.swift; sourceTree = "<group>"; };
5860
/* End PBXFileReference section */
5961

@@ -110,6 +112,7 @@
110112
children = (
111113
B19FD9A91FD51BE800A0864A /* main.swift */,
112114
B1AED4AF28369C94004B2A19 /* Icon.swift */,
115+
B1AED4B12836B1F2004B2A19 /* AppInfo.swift */,
113116
B108F89720EE3F230020BA08 /* Alert.swift */,
114117
B108F88A20EE33F90020BA08 /* URL.swift */,
115118
B108F88C20EE37240020BA08 /* Settings.swift */,
@@ -238,6 +241,7 @@
238241
B19FD9EB1FD5552800A0864A /* FinderApplication.swift in Sources */,
239242
B108F89020EE398C0020BA08 /* AppleTerminal.swift in Sources */,
240243
B1AED4B028369EA3004B2A19 /* Icon.swift in Sources */,
244+
B1AED4B22836B247004B2A19 /* AppInfo.swift in Sources */,
241245
B108F89420EE3A390020BA08 /* iTerm2.swift in Sources */,
242246
B19FD9F11FD5552800A0864A /* TerminalApplication.swift in Sources */,
243247
B19FD9D61FD538B000A0864A /* main.swift in Sources */,
@@ -369,7 +373,7 @@
369373
CODE_SIGN_IDENTITY = "-";
370374
CODE_SIGN_STYLE = Manual;
371375
COMBINE_HIDPI_IMAGES = YES;
372-
CURRENT_PROJECT_VERSION = 7;
376+
CURRENT_PROJECT_VERSION = 8;
373377
DEVELOPMENT_TEAM = "";
374378
INFOPLIST_FILE = Sources/OpenTerminal/Info.plist;
375379
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
@@ -392,7 +396,7 @@
392396
CODE_SIGN_STYLE = Manual;
393397
COMBINE_HIDPI_IMAGES = YES;
394398
COPY_PHASE_STRIP = YES;
395-
CURRENT_PROJECT_VERSION = 7;
399+
CURRENT_PROJECT_VERSION = 8;
396400
DEVELOPMENT_TEAM = "";
397401
INFOPLIST_FILE = Sources/OpenTerminal/Info.plist;
398402
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";

Sources/OpenTerminal/AppInfo.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// AppInfo.swift
3+
// OpenTerminal
4+
//
5+
// Created by Tomohiro Kumagai on 2022/05/20
6+
// Copyright © 2022 EasyStyle G.K. All rights reserved.
7+
//
8+
9+
import AppKit
10+
11+
struct AppInfo {
12+
13+
private static let buildVersionKey = "CFBundleVersion"
14+
private static let lastLaunchedBuildVersionKey = "LastLaunchedBuildVersion"
15+
16+
static var buildVersion: Int {
17+
18+
guard let rawVersion = Bundle.main.object(forInfoDictionaryKey: buildVersionKey) as? String else {
19+
20+
fatalError("Failed to get current build version.")
21+
}
22+
23+
guard let version = Int(rawVersion, radix: 10) else {
24+
25+
fatalError("Invalid build version: \(rawVersion)")
26+
}
27+
28+
return version
29+
}
30+
31+
static var lastLaunchedBuildVersion: Int {
32+
33+
get {
34+
35+
UserDefaults.standard.integer(forKey: lastLaunchedBuildVersionKey)
36+
}
37+
38+
set (version) {
39+
40+
UserDefaults.standard.set(version, forKey: lastLaunchedBuildVersionKey)
41+
}
42+
}
43+
44+
static var effectiveAppIcon: NSImage {
45+
46+
NSImage(named: "EffectiveAppIcon")!
47+
}
48+
49+
static var bundlePath: String {
50+
51+
Bundle.main.bundlePath
52+
}
53+
}

Sources/OpenTerminal/Icon.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
import AppKit
1010

1111
func updateAppIcon() {
12-
13-
let icon = NSImage(named: "EffectiveAppIcon")!
14-
let path = Bundle.main.bundlePath
1512

16-
NSWorkspace.shared.setIcon(icon, forFile: path)
13+
let buildVersion = AppInfo.buildVersion
14+
15+
guard buildVersion != AppInfo.lastLaunchedBuildVersion else {
16+
17+
return
18+
}
19+
20+
if NSWorkspace.shared.setIcon(AppInfo.effectiveAppIcon, forFile: AppInfo.bundlePath) {
21+
22+
AppInfo.lastLaunchedBuildVersion = buildVersion
23+
}
1724
}

0 commit comments

Comments
 (0)