开发者

What's wrong with this "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


Since you can't figure it out, here's how I would write that script...

--> 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


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...

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜