美文网首页
AppleScript-方法

AppleScript-方法

作者: 不写昵称 | 来源:发表于2018-07-29 17:55 被阅读0次

定义方法

on 方法名(参数列表)
return 返回值 -- 如果无返回值可不写
end 方法名

方法的调用可在定义前或定义后

注意:
1、在方法中使用的变量都是局部变量,不能使用外部的变量,其命名也不会和外部的变量造成冲突。

set x to 1  
on renameTest()  
    return x * 5  
end renameTest  
set y to renameTest() 

2、局部变量的命名和外部变量的命名无冲突,修改同名的局部变量的值对外部变量无影响。例如:

set x to 1
on renameTest()
    set x to 2
    return 10
end renameTest
set y to renameTest()
get x --输出1

举例:获取两个数中的较大值
on getMaxNumber(a, b)
    if a > b then
        return a
    else
        return b
    end if
end getMaxNumber
get getMaxNumber(2, 5)

在其他程序中调用本文件的方法

使用 of me
举例:

on test(a)   --该方法是当前程序的方法,即脚本编辑器的方法
    display dialog a
end test
tell application "Finder"
    test("测试方法") of me  -- 在Finder程序中调用,如果没有of me 会报错
end tell

调用.scpt脚本文件中的方法

步骤:
1、加载指定的.scpt脚本文件:load script 脚本文件
2、调用脚本文件中的方法

举例:

set scriptFilePath to choose file -- 选择某个脚本文件
set scriptContent to (load script scriptFilePath) -- 加载脚本文件内容

tell scriptContent
    try
        test("该方法为另一个脚本文件中的方法") -- 调用脚本文件中的方法
    on error error_message number errorNum
        display dialog "异常:" & error_message & "异常号:" & errorNum
    end try
end tell

相关文章

  • AppleScript-方法

    定义方法 on 方法名(参数列表)return 返回值 -- 如果无返回值可不写end 方法名 方法的调用可在定义...

  • AppleScript-指令

    say作用:让计算机发声 beep作用:让计算机发出‘咚’的声音。 tell作用:将特定的任务交付给Mac中特定的...

  • AppleScript-变量

    规则 变量只能有一个词组成中间不能有空格不能以数字开头允许使用下划线"_"不能使用关键字做变量名,如count关键...

  • AppleScript-异常捕获

    语法: 举例:

  • AppleScript-逻辑语句

    条件语句 举例: 循环语句 语法1:repeat 循环次数 timesend repeat举例: 语法2:repe...

  • AppleScript-文件及路径

    路径格式:硬盘:子路径:子路径如:"Macintosh HD:Users:henry:Desktop:未命名文件夹...

  • AppleScript-字符串

    使用规则字符串需使用双引号,不能使用单引号字符串中有引号时,需使用转义字符"",如"\他说:"你好!"" 字符串拼...

  • AppleScript-运算符

    算数运算符加、减、乘、除、乘方^ &运算符作用:合并字符串、合并数组如 \转义字符字符串中有引号时 字符串要显示“...

  • AppleScript-基本数据类型

    有4种基本数据类型:numer -- 数字String -- 字符串list ---数组record --相当于...

  • 方法,方法,方法。

    解决问题的方法。

网友评论

      本文标题:AppleScript-方法

      本文链接:https://www.haomeiwen.com/subject/nxhtvftx.html