这个“如果”有什么问题吗?

发布于 2024-09-05 10:36:20 字数 1897 浏览 5 评论 0原文

我目前正在尝试制作一个 AppleScript,它应该识别 Finder 中选定的文件并在终端中执行相关命令。

自从我到达应该定义所选文件的语言的部分以来,一切都很顺利:它只是不检查 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-09-12 10:36:20

既然你无法弄清楚,这就是我如何编写该脚本......

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

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
惯饮孤独 2024-09-12 10:36:20

你的代码是错误的。变量 finderSelection 中的 Finder 选择是一个列表。列表具有“项目”,因为列表可以容纳多个事物。因此,如果您想处理 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文