2010-06-13 5 views
1

J'essaye actuellement de faire un AppleScript qui devrait identifier le dossier choisi dans le Finder et faire la commande relative dans le terminal.Quel est le problème avec ce "si"?

Tout allait bien depuis que j'ai atteint la partie où il devrait définir la langue du fichier sélectionné: il ne vérifie pas les ifs. J'ai vérifié si elle écrit correctement FileExtension (via return) et c'est le cas.

Merci

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

Répondre

1

Puisque vous ne pouvez pas comprendre, voici comment j'écrire ce 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 
+0

Celui-ci va dans les ifs. Merci! – Gurzo

1

Votre code est erroné. La sélection du Finder dans la variable finderSelection est une liste. Une liste a des "items" car une liste peut contenir plus d'une chose. Donc, si vous voulez gérer plusieurs éléments sélectionnés dans le Finder, vous avez besoin d'une boucle de répétition et de vérifier chaque élément de la liste individuellement. Si vous ne voulez que le premier élément sélectionné, vous voulez "item 1" de la sélection. En tant que tel, vous voulez probablement quelque chose comme ça ...

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

J'ai besoin et que vous voulez un seul sélectionné fichier, alors merci pour votre réponse. Cependant, cela ne résout pas mon problème ni ne le change: le script ne semble pas entrer dans la case "Vérifier la langue" ifs et va directement au dernier. – Gurzo