-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportSystemProfile.applescript
170 lines (138 loc) · 5.17 KB
/
ExportSystemProfile.applescript
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
######################################################################
# Export Systemprofile
#
# Export a System Profile in XML format and save it to the users Desktop
#
# 2015-07-12
# Timo Kahle
#
# Changes
#
# v1.0 (2013-07-29)
# o Initial version
#
# v1.1 (2014-08-31)
# o Refactoring
# + Added Notification Center support for 10.9+
#
# v1.2 (2015-07-12)
# o Fixed a bug that rendered the exported SPX invalid
# We MUST NOT add pipe the error console to the standard console for the export (& " 2>&1")
#
#
######################################################################
# Application Properties
property SPXName : "Systemprofile.spx"
property appIcon : "applet.icns"
property appName : "Export SystemProfile"
property appVersion : "1.2"
property TIMEOUT_SEC : 3600 -- 60 minutes
# Command definitions
property cmd_SPExportBase : "system_profiler -detailLevel full -xml >"
# Main
on run
# Set environment
set dlgIcon to (path to resource appIcon)
set userDesktopPath to path to desktop
set userName to short user name of (system info)
set hostName to host name of (system info)
set macAddress to primary Ethernet address of (system info)
# Get current timestamp
set myDate to generateTimestamp()
# Define System Profile Export filename
set newMACAddress to replace_chars(macAddress, ":", "-")
set myTargetSPX to (POSIX path of userDesktopPath) & hostName & "-" & userName & "-" & newMACAddress & "-" & myDate & "_" & SPXName
set theCommand to cmd_SPExportBase & quoted form of myTargetSPX
set executeCMD to ExecCommand(theCommand)
# Catch errors
if executeCMD contains "Error: " then
# Report FAILURE
if NotificationCenterSupported() is false then
display dialog "Error while exporting SystemProfile to " & (POSIX path of userDesktopPath) & executeCMD with title appName & " " & appVersion buttons {"OK"} with icon dlgIcon giving up after 3
else
display notification "Error while exporting SystemProfile." subtitle executeCMD with title appName
delay 1
end if
else
# Report SUCCESS
if NotificationCenterSupported() is false then
display dialog "SystemProfile exported to " & (POSIX path of userDesktopPath) with title appName & " " & appVersion buttons {"OK"} with icon dlgIcon giving up after 3
else
display notification "SystemProfile exported to " & (POSIX path of userDesktopPath) subtitle (POSIX path of userDesktopPath) as text with title appName
delay 1
end if
end if
end run
##################################################################
# Helper functions
##################################################################
# Generate a timestamp
on generateTimestamp()
set dateNow to (current date)
set timeStamp to getFormattedDate(dateNow) & "-" & getFormattedTime(dateNow)
set myTimestamp to quoted form of timeStamp
return timeStamp
end generateTimestamp
# Format date
on getFormattedDate(myDate)
tell (myDate) to get ((its year) * 10000 + (its month as integer) * 100 + (its day)) as string
set formattedDate to (text 1 thru 4 of the result) & "" & (text 5 thru 6 of the result) & "" & (text 7 thru 8 of the result)
end getFormattedDate
# Format time
on getFormattedTime(myDate)
-- Get the "hour"
set timeStr to time string of (myDate)
set Pos to offset of ":" in timeStr
set theHour to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
-- Get the "minute"
set Pos to offset of ":" in timeStr
set theMin to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
--Get "AM or PM"
set Pos to offset of " " in timeStr
set theSfx to characters (Pos + 1) through end of timeStr as string
return (theHour & "" & theMin & "" & theSfx) as string
end getFormattedTime
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
# Run a command with admin privileges
on ExecCommandAdmin(thisAction)
try
#Introduce timeout to prevent timing out of large transfers
with timeout of TIMEOUT_SEC seconds
set returnValue to do shell script (thisAction & " 2>&1") with administrator privileges
end timeout
return returnValue
on error errMsg
return "Error: " & errMsg
end try
end ExecCommandAdmin
# Run a command without admin privileges
on ExecCommand(thisAction)
try
#Introduce timeout to prevent timing out of large transfers
#with timeout of TIMEOUT_SEC seconds
#set returnValue to do shell script (thisAction & " 2>&1")
set returnValue to do shell script (thisAction)
#end timeout
return returnValue
on error errMsg
return "Error: " & errMsg
end try
end ExecCommand
# Valid OS X version
on NotificationCenterSupported()
set strOSXVersion to system version of (system info)
considering numeric strings
#set IsMavericks to strOSXVersion ³ "10.9"
set IsMavericks to strOSXVersion is greater than or equal to "10.9"
end considering
return IsMavericks
end NotificationCenterSupported