跳轉到內容

AppleScript 程式設計/高階程式碼列表/重複

來自華夏公益教科書,開放的書籍,開放的世界

使用命令 repeat 可以建立重複的行為,如果使用得當,往往可以大大縮短程式碼。

它的基本格式如下

repeat 10 times
	say "Hello."
end repeat

它可以用於對放入指令碼並已轉換為程式的每個專案執行特定任務。

on open (ListItem)
	repeat with thisItem in ListItem
		-- bunch of kick*** code
	end repeat
end open

一個使用 repeat 函式的實際指令碼示例可以在這裡找到 這裡.

以下是一個程式碼示例,它將等待 Safari 中的頁面完全載入

tell application "Safari" to activate -- Brings Safari frontmost application.
repeat
	tell application "Safari"
		if source of document in window frontmost contains "</html>" then
			tell application "Script Editor" to activate
			exit repeat -- This will quit the loop.
		else
			delay 1 -- Will wait for 1 second.
		end if
	end tell
end repeat

這裡可以找到一個有用的來源:www.macobserver.com

Mac Observer 上所有關於 AppleScript 的系列文章都可以在 www.macobserver.com/tips/applescript/ 找到

下一頁:  | 上一頁: 顯示對話方塊
首頁: AppleScript 程式設計/高階程式碼列表
華夏公益教科書