我的新玩具-AppleScript(四)

我的新玩具-AppleScript(四),第1张

我本来以为之前几篇文章已经把AppleScript的基础讲的差不多了,但是自己研究过文档后,发现还是有很多需要补充的,所以又加了一篇。

就是一些特殊的关键字,类似于其他语言中的self,return等,有固定的含义;千万不要用它来自定义变量。

result:记录最近一个命返尘令执行的结果,如果命令没有结果,那么将会得到错误

it:指代最近的一个tell对象

me:这指代段脚本。用法举例path to me返回本脚本所在绝对路径

tab:用于string,一个制表位

return:用于string,一个换行

在AppleScript的字符串比较方式中,你可以设定比较的方式:上面considering和ignoring含义都是清晰的,一个用于加上xx特征,一个用户忽略某个特征;一个特征就是一个attribute。

atrribute应该为列表中的任意一个:

case 大小写

diacriticals 字母变调符号(如e和é)

hyphens 连字符(-)

numeric strings 数字化字符串(默认是忽略的),用于比较版本号时启用它空世绝。

punctuation 标点符号(,.?!等等,包括中文标点)

white space 空格

AppleScript是有选择对话框的,想想也是应有之义;下面是一个最简单的选择框:

display alert "这是一个警告" message "你上学迟到了" as warning choose from list {"这是第一个妞", "dsfggf"} with title "选择框" with prompt "请选择选项"

选择框有以下参数:

 直接参数 紧跟list类型参数,包含所有备选项

 title 紧跟text,指定对话框的标题

 prompt 紧跟text,指定提示信息

 default items 紧跟list,指定默认选择的项目

 empty selection allowed 后紧跟true表示允许不选 multiple selections allowed 后紧跟true表示允许多选

注:该方法不创建文件,只是返回一个路径

choose file name with prompt "指定提示信息"

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

注:其中prompt和default location参数同Choose File Name另外invisibles指定显示隐藏 文件,multiple selections allowed可以多选,showing package contents显示包内容,省略时 则不显示隐藏文件/不可多选/不显示包内容

注:除了type其它参数相同.

choose file of type {"txt"}

Alias指向文件的唯一ID,一般都用它 *** 作.

set myAlias2 to alias "Macintosh HD:Users:Nathan:Desktop:exam.txt"

path to 用于返回相对路径

path to documents folder --返回当前用户的“文档”文件夹绝对路径alias

path to library folder from system domain --返回系统的“资源库”绝对路径alias

文件读取用read,允许直接读取;但是写入文件之前必须先打开文件,打开文件是open for access FileName,写入文件用write...to语句,最后记得关闭文件close access filePoint。斗姿

set myFile to alias "Macintosh HD:Users:Nathan:Desktop:example.txt" read myFile

set aFile to alias "Macintosh HD:Users:Nathan:Desktop:example.txt" set fp to open for access aFile with write permission write "abc" to fp close access fp

最后写了一个文件的小例子:

on createMyTxt() --在桌面上创建一个文件,内部包含一个txt文件,并向txt内插入文件 make new folder at desktop with properties {name:"star"} make new file at folder "star" of desktop with properties {name:"star.txt"} end createMyTxt --向txt文件内写入内容 on writeTextToFile() set txtFile to alias "Macintosh HD:Users:star:Desktop:star:star.txt" set fp to open for access txtFile with write permission write "你好,这是一个txt文件" to fp close access fp end writeTextToFile createMyTxt() writeTextToFile()

希望能够加深大家对AppleScript的认识!

路径格式

硬盘:子路径:子路径

如: "Macintosh HD:Users:henry:Desktop:未命名文件夹 2"

注意:文件夹与文件夹间用:相连,而不岩键岩是/

选择文件

使用choose file,这样就粗御不用手动填充路径

打开文件夹

打开文件

使用文件替身打开文件好处

好处:当文件被移动或改名时,依然能够打开文件。因为替身记录的是文件id

如果路径前面的是file或folder,则表示返回的路径是文件或文件夹本身的存储位置。

移动文件

move to

举例:

将制定文件移亮仔到垃圾桶


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12282630.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存