J'ai écrit un petit service script pour ouvrir une fenêtre de terminal iTerm sur un dossier arbitraire du Finder.
Je veux qu'il vérifie si iTerm est en cours d'exécution, et si c'est le cas, qu'il ouvre la session du terminal dans un nouvel onglet au lieu de l'ouvrir dans un onglet existant.
Le script se présente comme suit :
on run {input, parameters}
set cdPath to "cd " & (quoted form of POSIX path of (input as string))
if application "iTerm" is running then
display notification "running"
tell application "iTerm"
set termWin to (current terminal)
tell termWin
launch session "Default"
tell the last session
write text cdPath
end tell
end tell
end tell
else
display notification "not running"
tell application "iTerm"
activate
set termWin2 to (current terminal)
tell termWin2
tell the last session
write text cdPath
end tell
end tell
end tell
end if
return input
end run
Le problème est que lorsque je lance le script en tant que service, il se comporte toujours comme si iTerm était déjà en cours d'exécution (en affichant la notification "en cours d'exécution"), même si iTerm est fermé et très clairement PAS en cours d'exécution.
Mais si je colle le même script à l'éditeur script (en mettant cdPath à un littéral, comme set cdPath to "cd /etc"
) et l'exécuter directement, il fonctionnera correctement, soit en ouvrant une nouvelle instance iTerm, soit en réutilisant une instance existante et en créant un nouvel onglet, et affichera les notifications correspondantes.
Qu'est-ce qui se passe ici ? Pourquoi l'exécution du script en tant que service détecterait l'application comme étant en cours d'exécution quoi qu'il arrive ?
Mise à jour
Si je simplifie le script pour simplement afficher les notifications comme ceci :
on run {input, parameters}
set cdPath to "cd " & (quoted form of POSIX path of (input as string))
if application "iTerm" is running then
display notification "running"
else
display notification "not running"
end if
return input
end run
Il se comportera comme prévu (en affichant 'running' ou 'not running' en conséquence).
Mais si j'ajoute la partie "tell application", elle passera toujours par la branche "running", quoi qu'il arrive.
Par exemple :
on run {input, parameters}
set cdPath to "cd " & (quoted form of POSIX path of (input as string))
if application "iTerm" is running then
display notification "running"
else
display notification "not running"
tell application "iTerm"
activate
end tell
end if
return input
end run
ouvrira toujours iTerm (même si le tell application "iTerm"
il est sur la branche "not running", mais affiche la notification "running", depuis la branche "is running"... La simple présence d'une "application tell" déclenchera l'ouverture de l'application puis l'exécution du service.
Y a-t-il un moyen de contourner ce problème ?
Merci et salutations.
0 votes
J'ai également rencontré ce problème et je l'ai trouvé très ennuyeux. Avez-vous trouvé des solutions récemment ? Merci.