Что не так с этим «если»? - PullRequest
1 голос
/ 13 июня 2010

В настоящее время я пытаюсь создать AppleScript, который должен распознавать выбранный файл в Finder и выполнять соответствующую команду в терминале.

Все шло нормально, так как я достиг той части, где он должен определять языквыбранный файл: он просто не проверяет ifs.Я проверил, правильно ли он пишет fileExtension (через return) и делает ли это.

Спасибо

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

1 голос
/ 13 июня 2010

Поскольку вы не можете понять это, вот как я написал бы этот сценарий ...

--> 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
1 голос
/ 13 июня 2010

Ваш код неверный.Выбор Finder в переменной finderSelection представляет собой список.В списке есть «элементы», потому что список может содержать более одной вещи.Поэтому, если вы хотите обрабатывать несколько выбранных элементов в 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...