Что случилось с этим, “если”?

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

Все шло прекрасное, так как я достиг части, где она должна определить язык выбранного файла: это просто не проверяет 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
1
задан Gurzo 13 June 2010 в 14:06
поделиться

2 ответа

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

--> 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
ответ дан 2 September 2019 в 23:48
поделиться

Ваш код неправильный. Выбор 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
1
ответ дан 2 September 2019 в 23:48
поделиться
Другие вопросы по тегам:

Похожие вопросы: