Check Input As Numbers Only Using Array

18 11 2007

set numList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
repeat with myCounter from 1 to (length of serialNo)
set myChar to item myCounter of serialNo
if myChar is not in numList then
display dialog "Please enter numbers only." buttons {"OK"} default button 1 with icon "iconWarning"
return
end if
end repeat





Write To File As UTF-8

10 11 2007

set thePath to ":Library:Application Support:MyApp:test.txt"
write "Hello World" to file thePath as «class utf8»

If you get an Expected expression but found unknown token. (-2741) error, just change your applescript file format. Format -> File Encoding -> Western (MacRoman)





Run Applescript in Carbon

7 11 2007

Found in Apple Developer Connection

Example

SimpleRunAppleScript(
"tell application \"Finder\"\n"
" activate\n"
" select folder \"Documents\" of startup disk\n"
" open selection\n"
"end tell");





Get Root Path

7 11 2007

set rootpath to path to startup disk as string
display dialog rootpath





Auto. Create & Save TextEdit File

3 11 2007

set folderpath to "Macintosh HD:Users:Phoenix:test:"
tell application "TextEdit"
set myDoc to document 1
set text of myDoc to "Hello World"
set filepath to folderpath & "hello.txt"
close myDoc saving yes saving in filepath
end tell





Minimize All Safari Windows

1 11 2007

tell application "Safari"
set allWindows to get windows
set n to count allWindows
repeat until n = 0
set miniaturized of window n to true
set n to n - 1
end repeat
end tell





Get Default Browser

1 11 2007

Does not work in Leopard

set bundleID to do shell script "defaults read com.apple.LaunchServices | grep -A5 \"http\""
if bundleID contains "com.apple.safari" then
tell application "Safari" to activate
else if bundleID contains "org.mozilla.firefox" then
tell application "Firefox" to activate
end if





Display Dialog

1 11 2007

Displays dialog with 2 buttons

display dialog "How are you?" buttons {"Good", "Sucks"} default button 1 with icon 1

Display dialog with text box
set question to display dialog "Enter your name" default answer "" buttons {"OK", "Close"} default button 1 with icon 1
set selectedButton to button returned of question
set strName to text returned of question
if selectedButton is equal to "OK" then
display dialog "Hello " & strName
end if





Launch Application using Finder

1 11 2007

Useful when location of application is unknown (or just can’t be arsed to find it :P )

tell application "Finder"
tell application NameOfMyApp to activate
end tell





Launch Website & Add Text Into Textbox

1 11 2007

tell application "Safari"
--activate to bring Safari to the front--
activate
try
--check if Safari is already open --
set currentDoc to the document of window 1
on error NSReceiverEvaluationScriptError
set currentDoc to make new docuent
end try
set the URL of currentDoc to "http://www.google.com"
-- or use--
open location "http://www.google.com"
--delay for page to load--
delay 2
--use javascript to enter text into textbox
set thisscript to "document.forms[0].q.value='applescript';"
do JavaScript thisscript in document 1
end tell








Follow

Get every new post delivered to your Inbox.