script lib

itunes

set myFavs to "Current Favorites"

tell application "Script Editor"
	set currentList to «class cPly» myFavs
	«event hookAdd » (get «class pLoc» of «class pTrk») given «class insh»:currentList
end tell
set playlist_ to text returned of result

tell application "Script Editor"
	make new «class cUsP» with properties {name:playlist_}

	set currentList to «class cPly» playlist_
	«event hookAdd » (get «class pLoc» of «class pTrk») given «class insh»:currentList
end tell

display dialog "Create Playlist" default answer "new"

list

set myListCount to count of {"eggs", "milk", "bread"}
set myRandom to (random number 100)

set mySum to addTwo(myListCount, myRandom)

on addTwo(x, y)
	return x + y
end addTwo
set myList to {"red", "blue", "yellow", "green", "orange", "purple"}

choose from list myList ¬
	with prompt ¬
	"Favorite color?" default items {"blue"} ¬
	with multiple selections allowed

set myColor to result
set volumes_ to {"0", "1", "2", "3", "4", "5", "6", "7", ¬
	"8", "9", "10"}

set myVolume to choose from list volumes_ ¬
	with prompt "Choose volume level:"

set volume myVolume

function

addTwo(20, 50)
addTwo(2, 5)
addTwo(-2, 5)

on addTwo(x, y)
	return x + y
end addTwo

on factorial(x)
	if x > 0 then
		return x * (factorial(x - 1))
	else
		return 1
	end if
end factorial

dialog

set name_ to the text returned of ¬
	(display dialog ¬
		"What is your first name?" default answer "")
display dialog ¬
	"what is your favorite color?" default answer "blue"
set myFolder to text returned of ¬
	(display dialog ¬
		"Choose new folder name" default answer "test")

set myDesktop to choose folder with prompt ¬
	¬
		"Save folder in location" default location (path to desktop folder)

createFolder(myFolder, myDesktop)

on createFolder(newFolder, ExistingFolder)
	tell application "Finder"
		make new folder at folder ExistingFolder ¬
			with properties {name:newFolder}
	end tell
end createFolder
display dialog "stop" with icon stop
display dialog "note" with icon note
display dialog "caution" with icon caution
tell application "Finder"
	set sourceFolder to choose folder with prompt ¬
		"Choose the source folder"
	set destFolder to choose folder with prompt ¬
		"Choose the destination folder"
	duplicate files of sourceFolder to destFolder
end tell
display dialog ¬
	"AppleScript rocks!" buttons {"Meh", "Right on!"} ¬
	default button "Right on!"
display dialog ¬
	"what is your favorite color?" buttons {"blue", "red", "yellow"} ¬
	default button "blue"
set myColor to button returned of result
display dialog myColor

file IO

choose file name with prompt ¬
	"Please choose a file name:" default name ¬
	"logfile.txt" default location (path to desktop folder)
choose folder with prompt ¬
	¬
		"Choose a folder:" default location (path to desktop folder)
set file_ to choose file

tell application "Script Editor"
	activate
	open file_
	set text_ to read file_
	«event R*chEntb» text_ given «class TabW»:4
end tell
set myFiles to ¬
	choose file with prompt ¬
		"Choose file(s):" default location (path to documents folder) ¬
		with multiple selections allowed
tell application "Finder"
	set files_ to ¬
		(choose file of type {"public.text"} ¬
			with multiple selections allowed)

end tell
tell application "Finder"
	set types_ to {"public.html", "public.jpeg"}
	set files_ to ¬
		(choose file of type types_ ¬
			with multiple selections allowed)
end tell
set file_ to choose file

tell application "Script Editor"
	activate
	open file_
	set text_ to read file_
	«event R*chDumQ» text_
end tell
set file_ to choose file

tell application "Script Editor"
	activate
	open file_
	set text_ to read file_
	«event R*chZapG» text_ with «class Znul», «class ZCtl» and «class Z8ch» given «class ZAct»:«constant ZActZdel»
end tell

set myLogFile to (path to desktop as string) & "test.log"

tell application "Finder"
	repeat while (file myLogFile exists) = false
	end repeat
end tell

display dialog "The log file is on the desktop!"
set destFolder to (path to current user folder)
display dialog "New Folder Name" default answer ""
set newFolder to the text returned of result

tell application "Finder"
	if (exists folder newFolder of destFolder) then
		display dialog "Sorry, that folder exists!"
	else
		make new folder at destFolder ¬
			with properties {name:newFolder}
		display dialog "New folder created!"
	end if
end tell
set files_ to ¬
	choose file with multiple selections allowed
set combine_ to ""

repeat with file_ in files_
	set content_ to read file_
	set combine_ to combine_ & return & content_
end repeat

set output_ to choose file name default name "combined.txt"
set reference_ to open for access output_ with write permission
write combine_ to reference_
close access reference_
tell application "Finder"
	get count of items of trash
end tell
set file_ to choose file
set path_ to POSIX path of file_
display dialog "Choose name of shortcut:" default answer ""
set shortcut_ to text returned of result

do shell script "ln -s " & path_ & " " & "~/Desktop/" & shortcut_
tell application "Finder"
	set folder_ to choose folder

	delete every item of folder_

end tell
tell application "Finder"
	set files_ to ¬
		choose file with multiple selections allowed

	repeat with file_ in files_
		set path_ to quoted form of POSIX path ¬
			of file_ as string
		do shell script "rm -rf " & path_
	end repeat

end tell
tell application "Finder"
	set files_ to ¬
		choose file with multiple selections allowed

	repeat with file_ in files_
		delete file_
	end repeat

end tell
tell application "Finder"
	set countTrashItems to count of items of trash

	if (countTrashItems > 100) then
		display dialog "Time to empty trash?" buttons {"Yes", "No"}
		if button returned of result = "Yes" then
			empty the trash
			display dialog "Just took out the trash!"
		else
			display dialog "OK, I'll leave the trash alone!"
		end if
	end if
end tell
set myPath to ¬
	choose file with prompt ¬
		"Trash which file?" default location (path to desktop folder)

trashIt(myPath)

on trashIt(itemPath)
	tell application "Finder"
		delete item itemPath
	end tell
end trashIt
tell application "Finder"
	set files_ to ¬
		choose file with multiple selections allowed

	display dialog ¬
		"Comment:" default answer "Spotlight comments"
	set comment_ to text returned of result

	repeat with file_ in files_
		set comment of file_ to comment_
	end repeat
end tell
set path_ to (choose file)
set saying_ to readFile(path_)

say saying_ using "Alex" saving to ":Users:myerman:Desktop:something.aiff"

on readFile(path_)
	set file_ to (open for access (path_))
	set txt_ to (read file_ for (get eof file_))
	close access file_
	return txt_
end readFile
set file_ to choose file of type {"public.text"}

tell application "TextEdit"
	open file_
end tell
tell application "Finder"
	set files_ to ¬
		choose file with multiple selections allowed

	repeat with file_ in files_
		open file_
	end repeat
end tell
set file_ to choose file

tell application "Script Editor"
	activate
	open file_
end tell
set file_ to choose file
set content_ to read file_

get paragraph 1 of content_
set file_ to choose file
set content_ to read file_

get paragraph 1 of content_
set file_ to choose file
set content_ to read file_

get some word of content_

set file_ to choose file
read file_
set file_ to choose file
set content_ to read file_

get words 1 thru 5 of content_
set folder_ to choose folder
set files_ to {}

tell application "Finder"
	set files_ to every item of folder_

end tell
set myDesktop to ¬
	(choose folder with prompt ¬
		"Choose a folder" default location (path to desktop folder))

set counter to countFolderItems(myDesktop)

on countFolderItems(folderName)
	tell application "Finder"
		set theFolder to (folder folderName)
		set myCount to the count of (every item of theFolder)
		return myCount
	end tell
end countFolderItems
tell application "Finder"
	get folders of (path to desktop pictures folder)
end tell
set file_ to choose file
set content_ to read file_

get character 3 of content_
set file_ to choose file
set content_ to read file_

text 11 thru 19 of content_
set file_ to choose file
set content_ to read file_

get characters 11 thru 19 of content_
tell application "Finder"
	set folder_ to choose folder
	
	delete every item of folder_
	
end tell

shell

display dialog "Choose file type:" default answer "txt"
set filetype_ to text returned of result

do shell script "find ~ -type f -name *." & filetype_ & " -print"

image

set file_ to choose file
set the target_ to (choose file name default name "newimage.gif")
set the path_ to the target_ as Unicode text

tell application "Image Events"
	launch
	set image_ to open file_
	save image_ as GIF in path_ with icon
	close image_
end tell
set file_ to choose file
set target_ to (choose file name default name "image.gif")
set path_ to the target_ as Unicode text

tell application "Image Events"
	launch
	set image_ to open file_

	--figure out which is greater, H or W
	save image_ as GIF in path_
	close image_
end tell
set file_ to choose file

tell application "Image Events"
	launch
	set image_ to open file_
	flip image_ with horizontal
	save image_
	close image_
end tell
set file_ to choose file
tell application "Image Events"
	launch
	set image_ to open file_
	copy the color space of image_ to space_
	close image_
end tell

display dialog "Space: " & (space_ as string)
set file_ to choose file
display dialog ¬
	"How much to resize?" default answer "400"
set size_ to text returned of result

tell application "Image Events"
	launch
	set image_ to open file_

	--figure out which is greater, H or W
	copy dimensions of image_ to {W, H}

	if W < H then
		set scalesize_ to (H * size_) / W
		set scalesize_ to round scalesize_ ¬
			rounding as taught in school
	else
		set scalesize_ to (W * size_) / H
		set scalesize_ to round scalesize_ ¬
			rounding as taught in school
	end if

	scale image_ to size scalesize_
	save image_
	close image_
end tell
set file_ to choose file
display dialog ¬
	"How much to resize?" default answer "400"
set size_ to text returned of result

tell application "Image Events"
	launch
	set image_ to open file_
	scale image_ to size size_
	save image_
	close image_
end tell
set file_ to choose file
display dialog "Angle of rotation?" default answer "90"
set angle_ to the text returned of result as integer

tell application "Image Events"
	launch
	set image_ to open file_
	rotate image_ to angle angle_
	save image_
	close image_
end tell
set file_ to choose file

tell application "Image Events"
	launch
	set image_ to open file_
	scale image_ by factor 0.5
	save image_
	close image_
end tell
set file_ to choose file
display dialog ¬
	¬
		"How much to scale? (0.1 to 1.0):" default answer "0.5"
set scale_ to text returned of result

tell application "Image Events"
	launch
	set image_ to open file_
	scale image_ by factor scale_
	save image_
	close image_
end tell
tell application "Photo Booth"
	activate
	tell application "System Events"
		keystroke "t" using {command down}
	end tell
end tell

loop

tell application "Finder"
	repeat with incrementValue from 1 to 5
		make new folder at desktop with properties ¬
			{name:incrementValue as string}
	end repeat
end tell
tell application "Finder"
	repeat with incrementValue from 1 to 50 by 5
		make new folder at desktop with properties ¬
			{name:incrementValue as string}
	end repeat
end tell
tell application "Finder"
	repeat with incrementValue from 10 to 1 by -1
		make new folder at desktop with properties ¬
			{name:incrementValue as string}
	end repeat
end tell
set myList to {"dogs", "cats", "cars", "planes", "food"}

tell application "Finder"
	repeat with noun in myList
		make new folder at desktop with properties ¬
			{name:noun as string}
	end repeat
end tell
set file_ to choose file
set content_ to read file_

set paras_ to every paragraph in content_
set nonblank_ to ""

repeat with para_ in paras_
	if length of para_ > 0 then
		set nonblank_ to nonblank_ & return & para_
	end if
end repeat
repeat 3 times
	display dialog ¬
		"Please enter your first name:" default answer ""
	set firstName to text returned of result
	if firstName is not equal to "" then exit repeat
end repeat

if firstName = "" then display dialog ¬
	"You never gave us your first name!"
repeat
	display dialog ¬
		"Please enter your first name:" default answer ""
	set firstName to text returned of result
	if firstName is not equal to "" then exit repeat
end repeat
set firstName to ""
repeat until firstName is not equal to ""
	display dialog ¬
		"Please enter your first name:" default answer ""
	set firstName to text returned of result
end repeat
repeat with myvol from 1 to 10
	set volume myvol
	beep
	delay 1
end repeat
set loopCounter to 1

repeat until loopCounter = 30
	display dialog loopCounter
	set loopCounter to loopCounter + 1
end repeat

include

set mathlib to (load script file ¬
	"myerman:library:scripts:myerman:mathlib.scpt")

tell mathlib
	factorial(10)
end tell

keystroke

set query_ to text returned of ¬
	(display dialog ¬
		"Look up in Dictionary" default answer "" buttons {"cancel", "look up word"} ¬
		default button 2)

tell application "Dictionary" to activate
tell application "System Events"
	tell process "Dictionary"
		set tf1_ to text field 1 of group 1 of toolbar 1 ¬
			of window "Dictionary"

		set value of tf1_ to ""
		keystroke "0" using command down

		tell tf1_
			keystroke query_
			keystroke return
		end tell
	end tell
end tell
set query_ to text returned of ¬
	(display dialog ¬
		"Look up in Dictionary" default answer "" buttons {"cancel", "look up word"} ¬
		default button 2)

tell application "Dictionary" to activate
tell application "System Events"
	tell process "Dictionary"
		set tf1_ to text field 1 of group 1 of toolbar 1 ¬
			of window "Dictionary"

		set value of tf1_ to ""

		tell tf1_
			keystroke query_
			keystroke return
		end tell
	end tell
end tell
set query_ to text returned of ¬
	(display dialog ¬
		"Look up in Dictionary" default answer "" buttons {"cancel", "look up word"} ¬
		default button 2)

tell application "Dictionary" to activate
tell application "System Events"
	tell process "Dictionary"
		set tf1_ to text field 1 of group 1 of toolbar 1 ¬
			of window "Dictionary"

		set value of tf1_ to ""
		keystroke "0" using command down

		tell tf1_
			keystroke query_
			keystroke return
		end tell
	end tell
end tell

branch


set randNumber to (random number (20)) as integer

if (randNumber > 10) then
	display dialog "More than 10: " & randNumber

else if (randNumber ≥ 7 and randNumber ≤ 9) then
	display dialog "Between 7 & 9: " & randNumber

else if (randNumber ≥ 3 and randNumber < 7) then
	display dialog "Between 3 and 6: " & randNumber

else
	display dialog "It's a 0, 1, or 2: " & randNumber

end if
set randNumber to (random number (20)) as integer

if (randNumber > 10) then
	display dialog "More than 10: " & randNumber
else

	if (randNumber ≥ 7 and randNumber ≤ 9) then
		display dialog ¬
			"Between 7 & 9: " & randNumber
	else
		if (randNumber ≥ 3 and randNumber < 7) then
			display dialog ¬
				"Between 3 and 6: " & randNumber
		else
			display dialog ¬
				"It's a 0, 1 or 2: " & randNumber
		end if
	end if
end if
set randNumber to (random number (20)) as integer

if (randNumber > 10) then
	display dialog "More than 10: " & randNumber
else
	display dialog "Less than 10: " & randNumber
end if
set randNumber to (random number (20)) as integer

if (randNumber > 10) then
	display dialog randNumber
end if

mail

display dialog ¬
	"enter an email address:" default answer "test@example.com"
set emailAddress to text returned of result

if not ((emailAddress contains "@") and ¬
	((emailAddress ends with ".com") or ¬
		(emailAddress ends with ".net") or ¬
		(emailAddress ends with ".org")) ¬
		and ¬
	((offset of "@" in emailAddress) > 1)) ¬
	then
	display dialog "Oops, that doesn't look right" with icon 0
else
	display dialog "Looks okay for now!"
end if
tell application "Contacts"
	set emailsList to {}
	set peopleCt to (count every person)
	repeat with i from 1 to peopleCt
		set emailsList to emailsList & (get value of every email ¬
			of person i)
	end repeat

	set filename to choose file name with prompt ¬
		"Save emails as text:" default name "emails.txt"

	set outfile to open for access filename with write permission
	repeat with thisaddress in emailsList
		write thisaddress & ", " to outfile
	end repeat
	close access outfile
end tell
tell application "Contacts"
	set people_ to every person ¬
		whose job title contains "manager"
end tell

tell application "Contacts"
	get every email of every person
end tell
tell application "Mail"
	check for new mail for account "Gmail"
end tell
script mydates
	
	on sayTodayString()
		return (current date) as string
	end sayTodayString
	
	on sayToday()
		return current date
	end sayToday
end script

tell mydates to sayTodayString()
tell application "Calendar"
	tell calendar "Invites"
		show (events where start date > (current date))
	end tell
end tell
tell application "Calendar"
	view calendar at (my (current date))
	switch view to day view
end tell

audio

display dialog ¬
	"Say something:" default answer "Something!"

set saying_ to text returned of result

say saying_ using "Alex" saving to ¬
	":Users:myerman:Desktop:something.aiff"

etc

tell application "Finder"
	set desktop picture to (some file of (path to desktop pictures folder))
end tell
tell application "Safari"
	set myUrl to URL of front document as string
end tell
set myUrl to "http://www.abc.com"

tell application "Safari"
	open location myUrl
	set allText to text of document of window 1
	display dialog allText
end tell
choose application with prompt ¬
	¬
		"Launch application(s)" with multiple selections allowed
tell application "Finder"
	set myDesktop to (path to desktop as string)
	-- other activities
	my launchSafari()
end tell
on launchSafari()
	tell application "Safari"
		activate
	end tell
end launchSafari
launchSafari()

on launchSafari()
	tell application "Safari"
		activate
	end tell
end launchSafari
posted @ 2023-04-11 11:23  fndefbwefsowpvqfx  阅读(15)  评论(0编辑  收藏  举报