-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathassignIcon.py
executable file
·148 lines (111 loc) · 3.06 KB
/
assignIcon.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python
#
# ----------------------------------------------------------------
#
# DESCRIPTION:
#
# A Script that assigns an icon to a file or a folder
#
# (c) Ali Rantakari, 2007
# http://hasseg.org
#
# ----------------------------------------------------------------
#
# REQUIRES:
#
# - OS X 10.4 (Tiger) or later
# - Developer Tools installed (found on OS discs)
#
# ----------------------------------------------------------------
#
#
import string, sys, os, shutil, tempfile
# ----------------------------------------------------------------
#
# SETTINGS:
#
#
# locations of CLI apps used
#
# system:
#
file = "/usr/bin/file"
osascript = "/usr/bin/osascript"
sips = "/usr/bin/sips"
#
# installed with developer tools:
#
setfile = "/Developer/Tools/SetFile"
rez = "/Developer/Tools/Rez"
derez = "/Developer/Tools/DeRez"
#
# all of the above in a list
#
usedcliapps = [file, osascript, sips, setfile, rez, derez]
# - - - - - - - - - - - - - - - - - - - - - -
# settings end here.
# ----------------------------------------------------------------
#
# FUNCTIONS:
def getMyBaseName():
return os.path.basename(sys.argv[0])
def displayUsage():
print "Usage: "+getMyBaseName()+" [image] [target]"
print " "
print " [image] is the image file to be used as the icon"
print " [target] is the target file or folder to assign"
print " the icon to"
print " "
def isImage(f):
o = os.popen(file+" -p \""+f+"\"", "r")
if (o.read().find("image") != -1):
return True
else:
return False
def runInShell(cmd):
os.popen(cmd, 'r')
# Script IMPLEMENTATION begins here ---------------------------
# -------------------------------------------------------------
#
# make sure all of the used CLI apps exist in
# their defined paths
for i in usedcliapps:
if not os.path.exists(i):
print "Error! "+i+" does not exist!"
print " "
sys.exit(127)
# make sure all required arguments are entered
if len(sys.argv)<3:
displayUsage()
sys.exit(0)
source = sys.argv[1]
target = sys.argv[2]
# validate argument types and paths
if os.path.exists(source):
if os.path.exists(target):
if isImage(source):
# args ok -> assign icon
tempfile.gettempdir()
shutil.copyfile(source, tempfile.tempdir+"/temp-pic")
runInShell(sips+" -i \""+tempfile.tempdir+"/temp-pic\"")
runInShell(derez+" -only icns \""+tempfile.tempdir+"/temp-pic\" > \""+tempfile.tempdir+"/temprsrc.rsrc\"")
if os.path.isdir(target):
runInShell(rez+" -append \""+tempfile.tempdir+"/temprsrc.rsrc\" -o \"`printf \""+target+"/Icon\\r\"`\"")
else:
runInShell(rez+" -append \""+tempfile.tempdir+"/temprsrc.rsrc\" -o \""+target+"\"")
runInShell(setfile+" -a C \""+target+"\"")
os.remove(tempfile.tempdir+"/temp-pic")
os.remove(tempfile.tempdir+"/temprsrc.rsrc")
else:
print "Error! "+source+" is not an image file"
print " "
sys.exit(127)
else:
print "Error! "+target+" does not exist"
print " "
sys.exit(127)
else:
print "Error! "+source+" does not exist"
print " "
sys.exit(127)