这个“如果”有什么问题吗?
我目前正在尝试制作一个 AppleScript,它应该识别 Finder 中选定的文件并在终端中执行相关命令。
自从我到达应该定义所选文件的语言的部分以来,一切都很顺利:它只是不检查 ifs。 我检查了它是否正确地写入了 fileExtension (通过返回)并且确实如此。
谢谢
--> Useful variables
set fileCheck to false
set languageCheck to false
set selectionCheck to false
set fileExtension to ""
set myCommand to ""
--> Get selected file
tell application "Finder"
if selection is {} then
tell application "Terminal"
activate
end tell
else
set finderSelection to selection as alias list
set selectionCheck to true
end if
end tell
--> Get file POSIX path
if selectionCheck is true then
set filePath to quoted form of POSIX path of finderSelection
end if
--> Get file extensions
if filePath contains "." then
set fileCheck to true
set fileExtension to text ((offset of "." in filePath) + 1) thru -1 of filePath
end if
--> Check language
-- No Extension
if fileCheck is false then
display dialog "warning:
the file you selected has no extension" buttons ("Ok") default button 1
-- Text
else if fileExtension is "txt" then
set myCommand to "open"
set languageCheck to true
-- Perl
else if fileExtension = "pl" then
set myCommand to "perl"
set languageCheck to true
-- Ruby
else if fileExtension is "rb" then
set myCommand to "ruby"
set languageCheck to true
-- Python
else if fileExtension is "py" then
set myCommand to "python"
set languageCheck to true
-- AppleScript
else if fileExtension is "scpt" then
set myCommand to "osascript"
set languageCheck to true
else
display dialog "warning:
the extension is not supported" buttons ("Ok") default button 1
end if
--> Terminal time!
if fileCheck is true and languageCheck is true then
do shell script "" & myCommand & " " & filePath
end if
I'm currently trying to make an AppleScript which should recognize the selected file in Finder and do the relative command in Terminal.
Everything was going fine since I reached the part where it should define the language of the selected file: it just doesn't check the ifs.
I checked if it writes fileExtension correctly (via return) and it does.
Thanks
--> Useful variables
set fileCheck to false
set languageCheck to false
set selectionCheck to false
set fileExtension to ""
set myCommand to ""
--> Get selected file
tell application "Finder"
if selection is {} then
tell application "Terminal"
activate
end tell
else
set finderSelection to selection as alias list
set selectionCheck to true
end if
end tell
--> Get file POSIX path
if selectionCheck is true then
set filePath to quoted form of POSIX path of finderSelection
end if
--> Get file extensions
if filePath contains "." then
set fileCheck to true
set fileExtension to text ((offset of "." in filePath) + 1) thru -1 of filePath
end if
--> Check language
-- No Extension
if fileCheck is false then
display dialog "warning:
the file you selected has no extension" buttons ("Ok") default button 1
-- Text
else if fileExtension is "txt" then
set myCommand to "open"
set languageCheck to true
-- Perl
else if fileExtension = "pl" then
set myCommand to "perl"
set languageCheck to true
-- Ruby
else if fileExtension is "rb" then
set myCommand to "ruby"
set languageCheck to true
-- Python
else if fileExtension is "py" then
set myCommand to "python"
set languageCheck to true
-- AppleScript
else if fileExtension is "scpt" then
set myCommand to "osascript"
set languageCheck to true
else
display dialog "warning:
the extension is not supported" buttons ("Ok") default button 1
end if
--> Terminal time!
if fileCheck is true and languageCheck is true then
do shell script "" & myCommand & " " & filePath
end if
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
既然你无法弄清楚,这就是我如何编写该脚本......
Since you can't figure it out, here's how I would write that script...
你的代码是错误的。变量 finderSelection 中的 Finder 选择是一个列表。列表具有“项目”,因为列表可以容纳多个事物。因此,如果您想处理 Finder 中的多个选定项目,那么您需要重复循环并单独检查列表中的每个项目。如果您只想要第一个选定的项目,那么您需要选择的“项目 1”。因此,你可能想要这样的东西......
Your code is wrong. The Finder selection in the variable finderSelection is a list. A list has "items" because a list can hold more than one thing. So if you want to handle multiple selected items in the Finder then you need a repeat loop and to check each item in the list individually. If you only want the first selected item then you want "item 1" of the selection. As such you probably want something like this...