Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

is there something akin to regEx in applescript, and if not, what's the alternative?

I need to parse the first 10 chars of a file name to see if they are all digits. The obvious way to do this is fileName =~ m/^d{10}/ but I'm not seeing anything regExy in the applescript reference, so, I'm curious what other options I have to do this validation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't despair, since OSX you can also access sed and grep through "do shell script". So:

set thecommandstring to "echo "" & filename & ""|sed "s/[0-9]\{10\}/*good*(&)/"" as string
set sedResult to do shell script thecommandstring
set isgood to sedResult starts with "*good*"

My sed skills aren't too crash hot, so there might be a more elegant way than appending *good* to any name that matches [0-9]{10} and then looking for *good* at the start of the result. But basically, if filename is "1234567890dfoo.mov" this will run the command:

echo "1234567890foo.mov"|sed "s/[0-9]{10}/*good*(&)/"

Note the escaped quotes " and escaped backslash \ in the applescript. If you're escaping things in the shell you have to escape the escapes. So to run a shell script that has a backslash in it you have to escape it for the shell like \ and then escape each backslash in applescript like \\. This can get pretty hard to read.

So anything you can do on the command line you can do by calling it from applescript (woohoo!). Any results on stdout get returned to the script as the result.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...