Thinker

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Thinker

Forum for story


    AppleScript语言初步

    markeloff
    markeloff
    无尽星空


    帖子数 : 322
    积分 : 5201
    威望 : 0
    注册日期 : 2011-09-06
    年龄 : 35
    地点 : Singapore

    AppleScript语言初步 Empty AppleScript语言初步

    Post by markeloff 2019-07-05, 4:03 pm

    -面向对象(对象、属性、命令)
    -不区分大小写
    -数据类型,Boolean (True, Flase),
    Number(Integer, Real),
    Text String,
    Date,
    Constant (yes, no, ask),
    List(列表型) 如:{1,2,3},{{1,2},{a,b,c},},{1,1.9, "text"}
    列表型数据由{}包裹,一个列表中可以再包含列表,形成多维列表,列表里的具体数 据可以是同类型的。
    Record(记录型)
    如:{firstName:"iDoraemon", lastName:"Nathan"} 记录就是带有名称的列表。记录中的每一项都有名称(标识符)。我们可以认为List
    是每个数据都是匿名的Record。Record也可以进一步包含另一个Record。 此例中,包含两个Text型数据 "iDoraemon"和 "Nathan",它们的标识符分别是
    firstName和lastName。通过of关键字可以得到想要的数据:
    firstName of {firstName:"iDoraemon", lastName:"Nathan"} 上面这行代码返回 "iDoraemon"。

    markeloff
    markeloff
    无尽星空


    帖子数 : 322
    积分 : 5201
    威望 : 0
    注册日期 : 2011-09-06
    年龄 : 35
    地点 : Singapore

    AppleScript语言初步 Empty Re: AppleScript语言初步

    Post by markeloff 2019-07-05, 4:03 pm

    确定数据类型-Class of 要确定一个数据到底是什么类型的,使用Class of语句
    class of "string" 此代码得到结果text。
    markeloff
    markeloff
    无尽星空


    帖子数 : 322
    积分 : 5201
    威望 : 0
    注册日期 : 2011-09-06
    年龄 : 35
    地点 : Singapore

    AppleScript语言初步 Empty Re: AppleScript语言初步

    Post by markeloff 2019-07-05, 4:06 pm

    运算符
    数学运算符(常见七种)
    AppleScript语言初步 Yunsua10

    运算符:
    =
    equals
    is equal

    is not
    isn't
    isn't equal [to]
    is not equal [to]
    doesn't equal

    >
    is greater than
    comes after
    is not less than or equal to

    <
    is less than
    comes before
    is not greater than or equal to

    Start with
    begin with

    end with

    contains

    does not contain

    is in
    is contained by

    is not in
    is not contained by


    &运算符

    “&”左边的数据类型为Text(文本型)时,结果为Text;存在报错可能
    “&”左边的数据类型为Record(记录型)时,结果为Record;存在报错可能
    “&”左边的数据类型为其他时,结果为List类型

    "Text" & 1 --结果:"Text1" (Text类型)
    1 & "Text" --结果:{1, "Text"} (List类型)

    {name:"a"} & "b" --结果:{name:"a", «class ktxt»:"b"} (Record类型,第二个元素匿名)
    3 & {name:"a"} --结果:{3, "a"} (List类型,且丢失标识符)


    {1, 2, 3} & {4, 5, 6} {1, 2, 3, 4, 5, 6}
    {1, 2, 3} & 4 & 5 & 6 {1, 2, 3, 4, 5, 6}
    {a:1, b:2} & {c:3} {a:1, b:2, c:3}

    --错误举例
    "Text" & {name:"a"} --错误!无法将Record类型数据转为文本
    {name:"a"} & 3 --错误!无法将Integer转换为Record
    markeloff
    markeloff
    无尽星空


    帖子数 : 322
    积分 : 5201
    威望 : 0
    注册日期 : 2011-09-06
    年龄 : 35
    地点 : Singapore

    AppleScript语言初步 Empty Re: AppleScript语言初步

    Post by markeloff 2019-07-05, 4:26 pm

    第六节 提取对象中的元素


    提取字符串中的字母或单词
    every character of "一个字符串" --结果:{"一", "个", "字", "符", "串"}每个字符
    characters of "A String" --结果:{"A", " ", "S", "t", "r", "i", "n", "g"}每个字符

    words of "A string" --结果:{"A", "string"}
    every word of "我的名字叫张三" --结果:{"我", "的", "名字", "叫", "张三"}

    characters 3 through 5 of "A String"
    words 3 through 5 of "我的名字叫张三"

    word 2 of "This is a text"
    character 3 of "This is a text"

    ninth character of "This is a text"

    特别注意:
    第一个字符的位置是1,而不是0!
    “through”可以缩写为“thru”
    不推荐用序数词方法获得一个单词或字符
    不推荐对于中文引用“word”相关语句
    markeloff
    markeloff
    无尽星空


    帖子数 : 322
    积分 : 5201
    威望 : 0
    注册日期 : 2011-09-06
    年龄 : 35
    地点 : Singapore

    AppleScript语言初步 Empty Re: AppleScript语言初步

    Post by markeloff 2019-07-05, 4:34 pm

    提取Finder文件列表

    tell application "Finder"
    every file of desktop --获得桌面上所有文件(List类型),其内容很详细
    files of desktop --同上

    every folder of desktop --获得桌面上所有文件夹(List类型),其内容很详细
    folders of desktop --同上

    name of every file of desktop --结果:获得桌面上所有文件名称(List类型) end tell


    tell application "Finder"
    every file of desktop whose name begins with "a" --仅获得文件名以a开头的文件列表
    every file of desktop where its name contains "a" --仅获得文件名包含a的文件列表
    end tell
    markeloff
    markeloff
    无尽星空


    帖子数 : 322
    积分 : 5201
    威望 : 0
    注册日期 : 2011-09-06
    年龄 : 35
    地点 : Singapore

    AppleScript语言初步 Empty Re: AppleScript语言初步

    Post by markeloff 2019-07-05, 4:36 pm

    第七节 添加注释和括号

    “--”
    “(*” + “*)”
    markeloff
    markeloff
    无尽星空


    帖子数 : 322
    积分 : 5201
    威望 : 0
    注册日期 : 2011-09-06
    年龄 : 35
    地点 : Singapore

    AppleScript语言初步 Empty Re: AppleScript语言初步

    Post by markeloff 2019-07-05, 4:36 pm

    第八节 代码缩写
    为了节省时间,将冗长的关键字缩写为几个字母也是非常有用的。此处举几例: application 简写为 app(编译时会补全)
    end tell/repeat/try 简写为 end(编译时会补全)
    through 简写为 thru
    if语句 可省略 then(将在下一章节介绍)
    markeloff
    markeloff
    无尽星空


    帖子数 : 322
    积分 : 5201
    威望 : 0
    注册日期 : 2011-09-06
    年龄 : 35
    地点 : Singapore

    AppleScript语言初步 Empty Re: AppleScript语言初步

    Post by markeloff 2019-07-05, 5:42 pm

    第二节 全局变量和局部变量

    通过事件处理器和脚本对象来理解

    第三节 数据共享机制
    基本数据类型的共享机制(不含Record和List)
    Record和List的数据共享机制
    >
    set a to {1, 2, 3, 4, 5} --定义a为List型数据
    set b to a --给变量b赋值为a的值(List型)
    结论:
    这次第二个对话框的结果似乎有点出乎意料,的确,对于List(包括Record)通
    过“set b to a”方式实现的是数据共享,而不是拷贝!实际上a和b指向的是内存中的同一个 位置,a和b只是同一内存位置的不同引用!

    copy关键字
    如果你想要List和Record数据交换方式和其他数据一样,那么您需要将“set”改
    为“copy”,需要注意,copy和set中的源数据和目标数据的位置是相反的,并且copy的目标 数据必须要事先定义。

    set a to {1, 2, 3, 4, 5}--定义a为List型数据
    set b to 1--需要提醒的是,使用copy之前必须先定义
    copy a to b--给变量b赋值为a的值(List型)
    display dialog "赋值的结果:a=" & a & "; b=" & b--显示a和b的值
    set item 1 of b to 0--修改List b中的第一个值为0
    display dialog "修改变量b之后:a=" & a & "; b=" & b--再次显示a和b的值

    运行后,两对话框分别显示: 赋值的结果:a=12345; b=12345 修改变量b之后:a=12345; b=02345

      Current date/time is 2024-04-26, 7:42 pm