这个“如果”有什么问题?

时间:2010-06-13 13:58:10

标签: applescript

我正在尝试制作一个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

2 个答案:

答案 0 :(得分:1)

你的代码错了。变量finderSelection中的Finder选择是一个列表。列表包含“项目”,因为列表可以包含多个项目。因此,如果要在Finder中处理多个选定项目,则需要重复循环并单独检查列表中的每个项目。如果您只想要第一个选定的项目,那么您需要选择的“项目1”。因此你可能想要这样的东西......

tell application "Finder"
    set finderSelection to selection as alias list
end tell
set firstItem to item 1 of finderSelection
set filePath to quoted form of POSIX path of firstItem
set fileExtension to text ((offset of "." in filePath) + 1) thru -1 of filePath

答案 1 :(得分:1)

既然你无法弄明白,我就是这样编写脚本......

--> Useful variables
set myCommand to missing value
set fileExtension to missing value

--> Get selected file
tell application "Finder"
 set finderSelection to selection
 if finderSelection is {} then
  display dialog "Warning: Nothing is selected!" buttons ("Ok") default button 1
  return
 else
  set theFile to item 1 of finderSelection
  set filePath to POSIX path of (theFile as text)
  set fileExtension to name extension of theFile
 end if
end tell

if fileExtension is "txt" then
 set myCommand to "open"

 -- Perl
else if fileExtension is "pl" then
 set myCommand to "perl"

 -- Ruby
else if fileExtension is "rb" then
 set myCommand to "ruby"

 -- Python
else if fileExtension is "py" then
 set myCommand to "python"

 -- AppleScript
else if fileExtension is "scpt" then
 set myCommand to "osascript"

else if fileExtension is not missing value then
 display dialog "Warning: the file is not supported" & return & return & filePath buttons ("Ok") default button 1
end if

--> Terminal time!
if myCommand is not missing value then
 do shell script myCommand & " " & quoted form of filePath
end if