Skip to content

Commit 48c9ccc

Browse files
committed
Introduce make file and apple script to bundle mac app
1 parent 3e6c568 commit 48c9ccc

File tree

8 files changed

+184
-0
lines changed

8 files changed

+184
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,4 @@ gradle-app.setting
200200
*.DS_Store
201201
.vscode/
202202
/libs
203+
*.iconset

Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
CYAN=\033[0;36m
2+
NC=\033[0m
3+
4+
APP_NAME=ExampleApp
5+
BUILD_DIR=./build
6+
TARGET=${BUILD_DIR}/${APP_NAME}.app
7+
JLINK_IMAGE=${BUILD_DIR}/image
8+
SKETCH_TOOL=/Applications/Sketch.app/Contents/Resources/sketchtool/bin/sketchtool
9+
10+
run:
11+
./gradlew build run
12+
13+
bundle:
14+
rm -rf ${BUILD_DIR}
15+
./gradlew jlink
16+
17+
@echo "\n> ${CYAN}Bundling Mac .app${NC}\n"
18+
osacompile -o ${TARGET} -s blueprint/launcher.applescript
19+
rm -f ${TARGET}/Contents/Info.plist
20+
cp ./blueprint/Info.plist ${TARGET}/Contents
21+
cp -R ${JLINK_IMAGE}/* ${TARGET}/Contents
22+
mv ${TARGET}/Contents/bin/* ${TARGET}/Contents/MacOS
23+
rm -rf ${TARGET}/Contents/bin
24+
25+
@echo "\n> ${CYAN}Add icon${NC}\n"
26+
rm -f ${TARGET}/Contents/Resources/droplet.icns
27+
cp ./blueprint/AppIcon.icns ${TARGET}/Contents/Resources
28+
29+
plist:
30+
plutil ./blueprint/Info.plist
31+
32+
icons:
33+
${SKETCH_TOOL} export artboards ./blueprint/AppIcon.sketch --output=./blueprint/AppIcon.iconset
34+
iconutil -c icns ./blueprint/AppIcon.iconset

blueprint/AppIcon.icns

122 KB
Binary file not shown.

blueprint/AppIcon.sketch

22.1 KB
Binary file not shown.

blueprint/Info.plist

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleExecutable</key>
6+
<string>droplet</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.example.pew</string>
9+
<key>CFBundleName</key>
10+
<string>Example App</string>
11+
<key>CFBundleIconFile</key>
12+
<string>AppIcon</string>
13+
<key>CFBundleVersion</key>
14+
<string>0.0.1</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>0.0.1</string>
17+
<key>CFBundleInfoDictionaryVersion</key>
18+
<string>6.0</string>
19+
<key>CFBundlePackageType</key>
20+
<string>APPL</string>
21+
<key>NSHighResolutionCapable</key>
22+
<true/>
23+
<key>LSRequiresCarbon</key>
24+
<true/>
25+
26+
<key>CFBundleDocumentTypes</key>
27+
<array>
28+
<dict>
29+
<key>CFBundleTypeExtensions</key>
30+
<array>
31+
<string>pew</string>
32+
<string>com.example.pew</string>
33+
</array>
34+
35+
<key>LSItemContentTypes</key>
36+
<array>
37+
<string>public.data</string>
38+
<string>com.example.pew</string>
39+
</array>
40+
41+
<key>CFBundleTypeOSTypes</key>
42+
<array>
43+
<string>****</string>
44+
</array>
45+
46+
<key>CFBundleTypeName</key>
47+
<string>com.example.pew</string>
48+
49+
<key>CFBundleTypeRole</key>
50+
<string>Viewer</string>
51+
52+
<key>CFBundleTypeIconFile</key>
53+
<string>AppIcon.icns</string>
54+
55+
<key>LSHandlerRank</key>
56+
<string>Default</string>
57+
58+
<key>LSIsAppleDefaultForType</key>
59+
<true/>
60+
</dict>
61+
</array>
62+
63+
<key>UTExportedTypeDeclarations</key>
64+
<array>
65+
<dict>
66+
<key>UTTypeIdentifier</key>
67+
<string>com.example.pew</string>
68+
69+
<key>UTTypeConformsTo</key>
70+
<array>
71+
<string>public.data</string>
72+
</array>
73+
74+
<key>UTTypeDescription</key>
75+
<string>Pew File</string>
76+
77+
<key>UTTypeIconFile</key>
78+
<string>AppIcon.icns</string>
79+
80+
<key>UTTypeTagSpecification</key>
81+
<dict>
82+
<key>public.filename-extension</key>
83+
<array>
84+
<string>pew</string>
85+
</array>
86+
</dict>
87+
</dict>
88+
</array>
89+
</dict>
90+
</plist>

blueprint/launcher.applescript

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(* INFO
2+
This applescript is used to bootstrap the resulting .app bundle and acts as a launcher.
3+
*)
4+
5+
property extension_list : {"pew"}
6+
property typeIDs_list : {"public.data", "com.example.pew"}
7+
8+
on open droppedItems
9+
set pathToApp to POSIX path of (path to me)
10+
repeat with i from 1 to count of droppedItems
11+
set currentItem to item i of droppedItems
12+
set itemInfo to info for currentItem
13+
14+
if folder of itemInfo is false then
15+
try
16+
set itemExtension to name extension of itemInfo
17+
on error
18+
set itemExtension to ""
19+
end try
20+
21+
try
22+
set itemType to type identifier of itemInfo
23+
on error
24+
set itemType to ""
25+
end try
26+
27+
if (folder of the itemInfo is false) and ((itemExtension is in the extension_list) or (itemType is in typeIDs_list)) then
28+
set filePath to the POSIX path of currentItem
29+
do shell script pathToApp & "Contents/MacOS/app " & quoted form of filePath & " > /dev/null 2>&1 &"
30+
end if
31+
end if
32+
end repeat
33+
end open
34+
35+
on run
36+
do shell script (POSIX path of (path to me)) & "Contents/MacOS/app > /dev/null 2>&1 &"
37+
end run

src/main/java/com/example/pew/App.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import javafx.scene.layout.VBox;
88
import javafx.stage.Stage;
99

10+
import javax.imageio.ImageIO;
1011
import java.awt.*;
1112
import java.io.File;
13+
import java.io.IOException;
1214

1315
public class App extends Application {
1416
/**
@@ -27,6 +29,8 @@ public class App extends Application {
2729

2830
@Override
2931
public void start(Stage stage) {
32+
initTaskbar();
33+
3034
// Check for files in provided console parameters
3135
Parameters params = getParameters();
3236
for (String param : params.getUnnamed()) Operator.parseInput(param);
@@ -49,6 +53,24 @@ public void start(Stage stage) {
4953
stage.show();
5054
}
5155

56+
/**
57+
* Attempts to update the mac task bar with an icon.
58+
*/
59+
private void initTaskbar() {
60+
if (!Taskbar.isTaskbarSupported()) return;
61+
62+
Image icon = null;
63+
64+
try {
65+
icon = ImageIO.read(getClass().getResourceAsStream("icon.png"));
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
70+
// Set the icon for the dock
71+
if (icon != null) Taskbar.getTaskbar().setIconImage(icon);
72+
}
73+
5274
public static void main(String[] args) {
5375
launch();
5476
}
53.1 KB
Loading

0 commit comments

Comments
 (0)