 |
 |
Free Applescript source code
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Feb 2001
Location: Huddersfield, UK
Status:
Offline
|
|
I just got into coding applescript, mainly using the shell to get information about the system. I just finished two basic scripts (neither of which work perfectly). One of them gets information about free disk space and the other gets the current uptime.
I just had a sudden urge to make the source code public in the hope that it might help some people learn the lovely language that is applescript! (I also used the web page as a learning experience to get my head round CSS, as you'll see!)
<a href="http://homepage.mac.com/richyfp/index.html" target="_blank">**Applescript source code**</a>
<small>[ 06-01-2002, 06:28 AM: Message edited by: Richyfp ]</small>
|
|
PM G4 DP 500 MHz, 768 Mb, DVD-ROM, 85 Gb, Mac OS X 10.3.9
PB G4 1.25 GHz, 512 Mb, DVD-R, 80 Gb, Mac OS X 10.4
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: May 2002
Status:
Offline
|
|
Hey, that's really cool! Thanks a lot, man! 
|
|
Love,
The Surfer
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Feb 2001
Location: Huddersfield, UK
Status:
Offline
|
|
No problem! I may post more scripts in the future as I write them.
I just hope that people find them useful for learning the slightly odd syntax/semantics of AppleScript (odd for a programmer, anyhow <img border="0" title="" alt="[Wink]" src="wink.gif" /> ) Of course, I'm still learning myself...
|
|
PM G4 DP 500 MHz, 768 Mb, DVD-ROM, 85 Gb, Mac OS X 10.3.9
PB G4 1.25 GHz, 512 Mb, DVD-R, 80 Gb, Mac OS X 10.4
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
Just as a tip.. never *ever* use the clipboard as a storage area. Other than that its pretty cool.. although I would opt for a more applescript approach than use the terminal except for the first string. Keep in mind that there are many variations on your uptime and you'd need around least 3 pages to accommodate them. However you can call the kernel from the terminal and get the number of seconds since the epoch and do your calculation from there. However there is no easy way to get the "current time" since the epoch. I tried and I got it within about 20 seconds so I dunno.. And while we're posting AppleScripts..
I've been meaning to add a representative character feature where "[img]www.site.com/bullet.jpg[/img]" would just bee one character. Anyways...
copy "Settings; Other
1; 2; 3; 4
OK; blah" to theText
copy return to AppleScript's text item delimiters
copy every text item of theText to theText
copy "; " to AppleScript's text item delimiters
repeat with i from 1 to length of theText
copy every text item of item i of theText to item i of theText
end repeat
copy {""} to AppleScript's text item delimiters
my tableize(theText, " ", false, 2, center)
on tableize(thelists, padChar, wrap, spacing, itemAlign)
if padChar is " " then copy " " to padChar
script scriptobject
property temp_list : {}
property length_list : {}
property widestList : 0
property widestString : 0
end script
copy { a reference to temp_list of scriptobject, a reference to length_list of scriptobject, a reference to widestList of scriptobject, a reference to widestString of scriptobject} to {temp_list, length_list, widestList, widestString}
if wrap is false then copy 0 to a
repeat with i from 1 to length of thelists
copy length of item i of thelists to listLength
if listLength > widestList then copy listLength to widestList
end repeat
repeat with i from 1 to length of thelists
repeat until (length of item i of thelists) is widestList
copy "" to (item i of thelists)'s end
end repeat
end repeat
repeat with i from 1 to length of thelists
repeat with i2 from 1 to length of item i of thelists
copy length of item i2 of item i of thelists to stringLength
if stringLength > widestString then copy stringLength to widestString
if wrap is false then if widestString > a then copy widestString to a
copy stringLength to temp_list's end
end repeat
copy {widestString, temp_list} to length_list's end
copy {} to temp_list
end repeat
if wrap then
copy {(item 1 of length_list)'s item 2, {}} to {refList, alignedList}
repeat with anEntry in length_list
repeat with z from 1 to length of refList
if item z of item 2 of anEntry > item z of refList then copy (item z of (item 2 of anEntry)) to (item z of refList)
end repeat
end repeat
repeat with anylist in thelists
copy {} to temp_list_two
copy 1 to y
repeat with thisstring in anylist
copy true to g
repeat until (length of thisstring) is ((item y of refList) + spacing)
if itemAlign is left then
copy thisstring & padChar to thisstring
else if itemAlign is right then
copy padChar & thisstring to thisstring
else
if g then
copy thisstring & padChar to thisstring
copy false to g
else
copy padChar & thisstring to thisstring
copy true to g
end if
end if
end repeat
copy {thisstring & "|", y + 1} to {temp_list_two's end, y}
end repeat
copy {temp_list_two} to alignedList's end
end repeat
repeat with i from 1 to length of alignedList
copy item i of alignedList as string to item i of alignedList
end repeat
copy return & "|" to AppleScript's text item delimiters
copy alignedList as string to alignedList
copy {""} to AppleScript's text item delimiters
return "|" & alignedList
else
copy (a + spacing) to a
copy {} to alignedList
copy 1 to y
repeat with anylist in thelists
copy {} to temp_list_two
repeat with thisstring in anylist
copy true to g
repeat until length of thisstring = (item 1 of item y of length_list) + a - (item 1 of item y of length_list)
if itemAlign is left then
copy thisstring & padChar to thisstring
else if itemAlign is right then
copy padChar & thisstring to thisstring
else
if g then
copy thisstring & padChar to thisstring
copy false to g
else
copy padChar & thisstring to thisstring
copy true to g
end if
end if
end repeat
copy temp_list_two & "|" & thisstring as string to temp_list_two
end repeat
copy temp_list_two & "|" to temp_list_two
copy {temp_list_two} to alignedList's end
copy (y + 1) to y
end repeat
copy {""} to AppleScript's text item delimiters
repeat with i from 1 to length of alignedList
copy item i of alignedList as string to item i of alignedList
end repeat
copy return to AppleScript's text item delimiters
copy alignedList as string to alignedList
copy {""} to AppleScript's text item delimiters
return alignedList
end if
end tableize
At the top you can modify theText to any data that's seperated by semi-colons and returns. You can set whether to align to left, center, or right. Whether you want it to wrap.. the additional spacing.. and the padding character. I've been meaning to write an app around this but I just haven't gotten to it :/
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Feb 2001
Location: Huddersfield, UK
Status:
Offline
|
|
Wow, Synotic - that's quite a script! You didn't post what it does.... <img border="0" title="" alt="[Wink]" src="wink.gif" /> but respect goes out to you - that's the biggest AppleScript I've ever seen.
Cheers for the tip about not using the clipboard. But if I don't use pbpaste from the Terminal and then get the value from the clipboard, how else can I get the uptime/disk information into the script? And yes, I realise that the script is terribly inefficient in that it is executing shell scripts everywhere, but I wasn't as familiar with AppleScript's string handling abilities as i am now!! I might recode some of it to make it more like an AppleScript than a shell script.
I've added a couple of more scripts to the page (which pale in comparison to Syn's script!) that open the Apache logs - I find this very useful when the scripts are launched from Script Runner or the script menu addition available from Apple's applescript page.
|
|
PM G4 DP 500 MHz, 768 Mb, DVD-ROM, 85 Gb, Mac OS X 10.3.9
PB G4 1.25 GHz, 512 Mb, DVD-R, 80 Gb, Mac OS X 10.4
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Richyfp:
<strong>Wow, Synotic - that's quite a script! You didn't post what it does.... <img border="0" title="" alt="[Wink]" src="wink.gif" /> but respect goes out to you - that's the biggest AppleScript I've ever seen.
Cheers for the tip about not using the clipboard. But if I don't use pbpaste from the Terminal and then get the value from the clipboard, how else can I get the uptime/disk information into the script? And yes, I realise that the script is terribly inefficient in that it is executing shell scripts everywhere, but I wasn't as familiar with AppleScript's string handling abilities as i am now!! I might recode some of it to make it more like an AppleScript than a shell script.
I've added a couple of more scripts to the page (which pale in comparison to Syn's script!) that open the Apache logs - I find this very useful when the scripts are launched from Script Runner or the script menu addition available from Apple's applescript page.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">To be fair, ellis from macscripter helped me through a lot of it it was a real learning experience. Although the first version was terribly inefficient. The above posted version is highly optimized and should take about ~1 second. As for what it does..  well it takes any data and turns it into a table, just ascii. It's useful when posting info on a board such as this one which does not allow HTML.
Now for your script.. well the short answer is.. don't use pbcopy! When you do "set a to b"... that's essentially running a command where the result is "b".. if you're a visual person..
set a to b
is really
set a to get (b)
and get is a command. So whenever you run a command it returns a result. In this case, b.
So modify your script like this...
copy word -1 of (do shell script "uptime | awk '{print $3}'") to my_uptime_days
copy (do shell script "uptime | awk '{print $5}'") to my_uptime_time
copy word 1 of my_uptime_time to my_uptime_hours
copy word -1 of my_uptime_time to my_uptime_minutes
if (do shell script "uptime | awk '{print $6}'") is "hrs,
" then
display dialog "Uptime is: " & my_uptime_days & " days, " & my_uptime_hours & " hours." buttons {"OK"} default button 1
else
display dialog "Uptime is: " & my_uptime_days & " days, " & my_uptime_hours & " hours and " & my_uptime_minutes & " minutes." buttons {"OK"} default button 1
end if
I'd say more but I have to go!
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|