美文网首页
15.AppleScript

15.AppleScript

作者: aofeilin | 来源:发表于2020-09-09 20:34 被阅读0次

    -- number类型. https://www.jianshu.com/p/a22084e87b09
    set xx to 2
    set y to 3
    get y
    get xx
    -- 字符串类型
    set strX to "hello world"
    set strY to "APPleScript"
    -- 字符串拼接
    set strXY to strX & strY
    -- 获取字符串长度
    set lengthofstrXY to the length of strXY
    -- 分割成单个字符 并组成一个新的列表。
    set charList to every character of strXY
    -- number 与string类型转换
    set numberToString to 100 as string
    set stringToNumber to "1234" as number
    -- 通过 AppleScript's text item delimiters 来指定分隔号,然后通过 every text item of 来实现分割
    set AppleScript's text item delimiters to " "
    set defaultDelimiters to every text item of strXY
    -- list类型可以存放不同类型
    set firstList to {100, 200.0, "djfif", -10}
    set emptyList to {}
    set currentList to {2, 3, 4, 5}
    -- 列表拼接
    set modifiedList to firstList & emptyList & currentList
    -- 获取和更改列表中的元素
    set item 2 of modifiedList to "2"
    get modifiedList
    set the third item of modifiedList to "abcde"
    get modifiedList

    -- 用列表中的随机元素赋值
    set randomX to some item of modifiedList

    -- 获取最后一个元素
    set lastItem to the last item of modifiedList

    -- 负数表示从列表尾端开始获取元素
    set aLastItem to item -2 of modifiedList

    -- 获取第一个元素
    set firstItem to the first item of modifiedList

    set longList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    set shortList to items 6 through 8 of longList

    -- 逆向获取子列表
    set reversedList to reverse of longList
    set listCount to the count of longList
    set the end of longList to 5
    get longList

    -- 将 string 转换为 list
    set string1 to "string1"
    set stringList to string1 as list

    -- 可以用&将字符串和列表连接起来,结果取决于&前面的变量
    set strAndList to string1 & stringList

    --record 类型
    set aRecord to {name1:100, name2:"This is a record"}
    set valueOfName1 to the name1 of aRecord
    set newRecord to {name1:name1 of aRecord}
    set numberOfProperties to the count of aRecord
    --条件/循环

    set x to 500

    if x > 100 then
    -- display alert "x > 100"
    else if x > 10 then
    -- display alert "x > 10"
    else
    -- display alert "x <= 10"
    end if

    set sum to 0
    set i to 0
    repeat 100 times
    set i to i + 1
    set sum to sum + i
    end repeat
    get sum

    repeat with counter from 0 to 10 by 2
    -- display dialog counter
    end repeat

    set counter to 0
    set listToSet to {}
    -- 注意下这个 ≠ 符号是使用 Option+= 输入的
    repeat while counter ≠ 10
    -- display dialog counter as string
    set listToSet to listToSet & counter
    set counter to counter + 2
    end repeat
    get listToSet

    set counter to 0
    set listToSet to {}
    -- 注意下这个 ≠ 符号是使用 Option+= 输入的
    repeat while counter ≠ 10
    -- display dialog counter as string
    set listToSet to listToSet & counter
    set counter to counter + 2
    end repeat
    get listToSet

    set counter to 0
    set listToSet to {}
    repeat until counter = 10
    -- display dialog counter as string
    set listToSet to listToSet & counter
    set counter to counter + 2
    end repeat
    get listToSet

    set aList to {1, 2, 8}
    repeat with anItem in aList
    -- display dialog anItem as string
    end repeat

    -- 注释
    (*
    这是多行的注释
    这是多行的注释
    *)

    (*
    -- 按钮
    on showAlert(alertStr)
    -- display alert alertStr buttons {"I know", "Cancel"} default button "I know"
    end showAlert

    showAlert("hello world")

    *)
    -- 键盘使用组合键 Option+L 输入'¬' 可以实现代码折行

    (*
    on showAlert(alertStr)
    display alert alertStr ¬
    buttons {"I know", "Cancel"} default button "I know"
    end showAlert
    showAlert("hello world")
    *)
    -- input
    -- set dialogString to "Input a number here"
    -- set returnedString to display dialog dialogString default answer ""
    -- get returnedString

    (*
    set dialogString to "Input a number here"
    set returnedString to display dialog dialogString default answer ""
    set returnedNumber to the text returned of returnedString
    try
    set returnedNumber to returnedNumber as number
    set calNumber to returnedNumber * 100
    -- display dialog calNumber
    on error the error_message number the error_number
    -- display dialog "Error:" & the error_number & " Details:" & the error_message
    end try
    )
    (

    ignoring case
    if "AAA" = "aaa" then
    display alert "AAA equal aaa when ignoring case"
    end if
    end ignoring

    considering numeric strings
    if "10.3" > "9.3" then
    display alert "10.3 > 9.3"
    end if
    end considering
    *)

    -- display alert "这是一个警告" message "这是警告的内容" as warning

    -- choose from list {"选项1", "选项2", "选项3"} with title "选择框" with prompt "请选择选项"
    -- choose file name with prompt "指定提示信息"

    -- choose folder with prompt "指定提示信息" default location file "Macintosh HD:Users" with invisibles, multiple selections allowed and showing package contents

    -- choose file of type {"txt"}

    (*
    --创建文件
    --在桌面上创建一个文件,内部包含一个txt文件,并向txt内插入文件
    on createMyTxt()
    tell application "Finder"
    make new folder at desktop with properties {name:"star"}
    make new file at folder "star" of desktop with properties {name:"star.txt"}
    end tell
    end createMyTxt
    createMyTxt()

    --写入文件
    --向txt文件内写入内容
    on writeTextToFile()
    set txtFile to alias "Macintosh HD:Users:ex-zhangfanglin001:Desktop:star:star.txt"
    set fp to open for access txtFile with write permission
    write "你好,这是一个txt文件" to fp as «class utf8»
    close access fp
    end writeTextToFile

    writeTextToFile()
    *)

    (*
    tell application "Finder"
    empty the trash
    beep
    -- 打开启动磁盘
    open the startup disk
    end tell
    *)
    -- 调用 mac 的通知中心

    -- display notification "message" with title "title" subtitle "subtitle"

    -- You can use any of the voices from the System Voice pop-up on the Text to Speech tab in the Speech preferences pane.
    -- Default Value:
    -- The current System Voice (set in the Speech panel in System Preferences.
    (*
    tell current application
    say "My name is LiMei. Nice to meet you. How are you?" using "Veena"
    say "Fine, thanks. And you?" using "Victoria"
    say "我跟你说" using "Sin-Ji"
    end tell

    beep
    *)

    set urlMyBlog to "https://blog.csdn.net/sodaslay"
    set urlChinaSearch to "http://www.chinaso.com"
    set urlBiying to "https://cn.bing.com"

    --使用Chrome浏览器
    tell application "Google Chrome"
    --新建一个chrome窗口
    set window1 to make new window
    tell window1
    --当前标签页加载必应,就是不用百度哈哈
    set currTab to active tab of window1
    set URL of currTab to urlBiying
    --打开csdn博客,搜索
    make new tab with properties {URL:urlMyBlog}
    make new tab with properties {URL:urlChinaSearch}
    --将焦点由最后一个打开的标签页还给首个标签页
    set active tab index of window1 to 1
    end tell
    end tell

    相关文章

      网友评论

          本文标题:15.AppleScript

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