1+ -- AppleScript 示例代码 - CodeForge 代码执行环境
2+
3+ log " Welcome to CodeForge!"
4+ log ""
5+
6+ log " ========================================="
7+ log " CodeForge AppleScript "
8+ log " ========================================="
9+ log ""
10+
11+ -- 基本输出示例
12+ log " ✅ AppleScript 运行成功! (AppleScript is working!)"
13+ log " 🍎 这是 AppleScript 脚本 (This is AppleScript script)"
14+ log ""
15+
16+ -- 变量操作
17+ set appName to " CodeForge"
18+ set version to " AppleScript"
19+ set number1 to 10
20+ set number2 to 20
21+ set calcResult to number1 + number2
22+
23+ log " 🔢 简单计算 (Simple calculation):"
24+ log (number1 as string ) & " + " & (number2 as string ) & " = " & (calcResult as string )
25+ log ""
26+
27+ -- 字符串操作
28+ log " 📝 字符串操作 (String operations):"
29+ log " 平台名称 (Platform): " & appName
30+ log " 语言版本 (Language): " & version
31+ log " 完整信息 (Full info): " & appName & " - " & version
32+ log ""
33+
34+ -- 循环示例
35+ log " 🔄 循环输出 (Loop output):"
36+ repeat with i from 1 to 5
37+ log " 第 " & (i as string ) & " 次输出 (Output #" & (i as string ) & " ): Hello from CodeForge!"
38+ end repeat
39+ log ""
40+
41+ -- 列表操作
42+ set fruits to {" 苹果" , " 香蕉" , " 橙子" , " 葡萄" }
43+ log " 🍎 水果列表 (Fruit list):"
44+ repeat with i from 1 to count of fruits
45+ set fruit to item i of fruits
46+ log (i as string ) & " . " & fruit
47+ end repeat
48+ log ""
49+
50+ -- 条件判断
51+ set score to 85
52+ log " 📊 成绩评估 (Score evaluation):"
53+ if score ≥ 90 then
54+ log " 优秀! (Excellent!)"
55+ else if score ≥ 80 then
56+ log " 良好! (Good!)"
57+ else if score ≥ 60 then
58+ log " 及格 (Pass)"
59+ else
60+ log " 需要努力 (Need improvement)"
61+ end if
62+ log ""
63+
64+ -- missing value 示例
65+ set optionalValue to 42
66+ log " 🔍 missing value 示例 (missing value example):"
67+ if optionalValue is not missing value then
68+ log " 可选值: " & (optionalValue as string ) & " (Optional value: " & (optionalValue as string ) & " )"
69+ else
70+ log " 值为空 (Value is missing)"
71+ end if
72+ log ""
73+
74+ -- 处理程序(Handler)示例
75+ on greetUser (userName )
76+ return " Hello, " & userName & " ! 👋"
77+ end greetUser
78+
79+ log " 🎭 处理程序示例 (Handler example):"
80+ set greeting to greetUser(" CodeForge用户" )
81+ log greeting
82+ log ""
83+
84+ -- 记录(Record)示例
85+ log " 👤 记录示例 (Record example):"
86+ set person to {name :" 张三" , age :25 , city :" 北京" }
87+ log " 姓名: " & (name of person) & " , 年龄: " & (age of person as string ) & " , 城市: " & (city of person)
88+ log ""
89+
90+ -- 日期和时间示例
91+ log " ⏰ 日期时间示例 (Date and time example):"
92+ set currentDate to (current date )
93+ log " 当前日期: " & (currentDate as string )
94+ set currentTime to time of currentDate
95+ log " 当前时间: " & (currentTime as string )
96+ log ""
97+
98+ -- 数学运算示例
99+ log " 📐 数学运算示例 (Math operations):"
100+ set mathResult1 to 2 ^ 3 -- 幂运算
101+ set mathResult2 to 17 mod 5 -- 取模运算
102+ set mathResult3 to round (22 / 7 ) -- 四舍五入
103+ log " 2的3次方: " & (mathResult1 as string )
104+ log " 17除以5的余数: " & (mathResult2 as string )
105+ log " 22/7四舍五入: " & (mathResult3 as string )
106+ log ""
107+
108+ -- 字符串操作示例
109+ log " 📝 字符串操作示例 (String operations):"
110+ set originalText to " CodeForge AppleScript Example"
111+ set upperText to do shell script " echo '" & originalText & " ' | tr '[:lower:]' '[:upper:]'"
112+ set wordCount to count of words of originalText
113+ log " 原始文本: " & originalText
114+ log " 大写文本: " & upperText
115+ log " 单词数量: " & (wordCount as string )
116+ log ""
117+
118+ -- 文件操作示例(安全的只读操作)
119+ log " 📁 文件信息示例 (File info example):"
120+ try
121+ set homeFolder to (path to home folder) as string
122+ set desktopPath to homeFolder & " Desktop:"
123+ log " 桌面路径: " & desktopPath
124+
125+ -- 获取桌面文件夹信息
126+ tell application " Finder"
127+ set folderInfo to get info for folder desktopPath
128+ set folderSize to size of folderInfo
129+ log " 桌面文件夹大小: " & (folderSize as string ) & " bytes"
130+ end tell
131+ on error errMsg
132+ log " 文件操作错误: " & errMsg
133+ end try
134+ log ""
135+
136+ -- 应用程序交互示例
137+ log " 💻 应用程序交互示例 (Application interaction):"
138+ try
139+ tell application " System Events"
140+ set appList to name of every process whose visible is true
141+ set runningAppCount to count of appList
142+ log " 当前运行的可见应用程序数量: " & (runningAppCount as string )
143+
144+ -- 显示前3个应用程序名称
145+ repeat with i from 1 to 3
146+ if i ≤ runningAppCount then
147+ set appName to item i of appList
148+ log " 应用程序 " & (i as string ) & " : " & appName
149+ end if
150+ end repeat
151+ end tell
152+ on error errMsg
153+ log " 应用程序查询错误: " & errMsg
154+ end try
155+ log ""
156+
157+ -- 系统信息示例
158+ log " 🖥️ 系统信息示例 (System info example):"
159+ try
160+ set systemInfo to system info
161+ set osVersion to system version of systemInfo
162+ set computerName to computer name of systemInfo
163+ log " 操作系统版本: " & osVersion
164+ log " 计算机名称: " & computerName
165+ on error errMsg
166+ log " 系统信息获取错误: " & errMsg
167+ end try
168+ log ""
169+
170+ -- 错误处理示例
171+ log " 🚨 错误处理示例 (Error handling):"
172+ try
173+ set riskyNumber to random number from 1 to 10
174+ if riskyNumber < 5 then
175+ error " 随机数太小了: " & (riskyNumber as string )
176+ else
177+ log " 随机数正常: " & (riskyNumber as string )
178+ end if
179+ on error errMsg number errNum
180+ log " 捕获错误 (" & (errNum as string ) & " ): " & errMsg
181+ end try
182+ log ""
183+
184+ -- 用户输入示例(注释版本,避免阻塞执行)
185+ log " 💬 用户交互概念示例 (User interaction concept):"
186+ -- set userInput to text returned of (display dialog "请输入您的名字:" default answer "用户")
187+ -- log "用户输入: " & userInput
188+ log " (实际运行时可以取消注释上面的代码进行用户交互)"
189+ log ""
190+
191+ -- 通知示例
192+ log " 🔔 通知示例 (Notification example):"
193+ try
194+ display notification " CodeForge AppleScript 执行完成!" with title " CodeForge" subtitle " AppleScript 示例" sound name " Glass"
195+ log " 通知已发送"
196+ on error
197+ log " 通知发送失败(可能需要权限)"
198+ end try
199+ log ""
200+
201+ -- AppleScript 独有的tell块示例
202+ log " 📱 Tell 块示例 (Tell block example):"
203+ tell application " Finder"
204+ set trashCount to count of items in trash
205+ log " 废纸篓中的项目数量: " & (trashCount as string )
206+ end tell
207+
208+ -- 使用 shell 脚本增强功能
209+ log " 🐚 Shell 脚本集成示例 (Shell script integration):"
210+ set shellOutput to do shell script " date +%Y-%m-%d"
211+ log " 今天日期 (通过shell): " & shellOutput
212+
213+ set unameOutput to do shell script " uname -s"
214+ log " 操作系统内核: " & unameOutput
215+ log ""
216+
217+ -- 修复的列表处理示例
218+ log " 📋 列表处理示例 (List processing):"
219+ set numberList to {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }
220+ set evenNumbers to {}
221+ set sumTotal to 0
222+
223+ -- 使用索引循环而不是 "repeat with num in list"
224+ repeat with i from 1 to count of numberList
225+ set currentNum to item i of numberList
226+ set sumTotal to sumTotal + currentNum
227+ if (currentNum mod 2 ) = 0 then
228+ set end of evenNumbers to currentNum
229+ end if
230+ end repeat
231+
232+ log " 原始列表: " & my listToString(numberList)
233+ log " 偶数: " & my listToString(evenNumbers)
234+ log " 总和: " & (sumTotal as string )
235+ log ""
236+
237+ -- 辅助处理程序:将列表转换为字符串
238+ on listToString (lst )
239+ set AppleScript 's text item delimiters to " , "
240+ set stringOutput to lst as string
241+ set AppleScript 's text item delimiters to ""
242+ return stringOutput
243+ end listToString
244+
245+ -- 文本分割示例
246+ log " ✂️ 文本分割示例 (Text splitting):"
247+ set sampleText to " Apple,Banana,Orange,Grape"
248+ set AppleScript 's text item delimiters to " ,"
249+ set fruitList to text items of sampleText
250+ set AppleScript 's text item delimiters to ""
251+
252+ log " 原始文本: " & sampleText
253+ repeat with i from 1 to count of fruitList
254+ log " 水果 " & (i as string ) & " : " & (item i of fruitList)
255+ end repeat
256+ log ""
257+
258+ -- 额外的列表操作示例
259+ log " 🔄 更多列表操作示例 (More list operations):"
260+ set originalList to {5 , 2 , 8 , 1 , 9 , 3 }
261+ set sortedList to my sortList(originalList)
262+ set reverseList to reverse of originalList
263+ log " 原始列表: " & my listToString(originalList)
264+ log " 反转列表: " & my listToString(reverseList)
265+ log " 排序列表: " & my listToString(sortedList)
266+ log ""
267+
268+ -- 简单的排序函数
269+ on sortList (inputList )
270+ set sortedList to {}
271+ repeat with i from 1 to count of inputList
272+ set end of sortedList to item i of inputList
273+ end repeat
274+
275+ -- 简单的冒泡排序
276+ repeat with i from 1 to count of sortedList
277+ repeat with j from 1 to (count of sortedList) - 1
278+ if item j of sortedList > item (j + 1 ) of sortedList then
279+ set temp to item j of sortedList
280+ set item j of sortedList to item (j + 1 ) of sortedList
281+ set item (j + 1 ) of sortedList to temp
282+ end if
283+ end repeat
284+ end repeat
285+
286+ return sortedList
287+ end sortList
288+
289+ log " 🎯 CodeForge AppleScript 代码执行完成!"
290+ log " 🎯 CodeForge AppleScript execution completed!"
291+ log ""
292+ log " 感谢使用 CodeForge 代码执行环境! 🚀"
293+ log " Thank you for using CodeForge! 🚀"
0 commit comments