【问题标题】:OSX: How can check whether a file exists in current directory using applescript?OSX:如何使用applescript检查当前目录中是否存在文件?
【发布时间】:2013-06-26 19:56:47
【问题描述】:

我想制作一个在当前目录中创建一个空文件的自动化应用程序。 我做了一些谷歌搜索,发现:
http://hints.macworld.com/article.php?story=20050219134457298http://hints.macworld.com/article.php?story=20100509134904820

但是,我想做一些更强大的事情。 如果指定的文件已经存在,我想显示警告而不是覆盖原始文件,这就是上述链接之一。 (另一个使用 textEdit 创建一个文本文件。我不想创建文本文件。我想要一个像 linux/unix 那样的空文件)

我已经想出了大部分的方法,但是

  1. 如何使用applescript检查文件是否存在于当前目录??
  2. 如何在 applescript 中连接两个变量?

【问题讨论】:

    标签: macos bash shell applescript automator


    【解决方案1】:

    我最近经常使用的东西是命令 /bin/test 本例中是否存在文件的测试测试

        if (do shell script "/bin/test -e " & quoted form of  (POSIX path of theFile)  & " ; echo $?") is "1" then
    -- 1 is false
    --do something
    
    end if
    

    -e 选项:

    -e file 如果文件存在则为真(无论类型如何)。

    /bin/test man page 中显示了许多其他测试选项

    【讨论】:

      【解决方案2】:

      另一个不需要 Finder 或系统事件的选项是尝试将 POSIX 文件或文件对象强制为别名:

      try
          POSIX file "/tmp/test" as alias
          true
      on error
          false
      end try
      

      【讨论】:

        【解决方案3】:

        以下代码改编自您的第二个链接,通常是正确的,但并不总是有效。当前目录最好指定为正在打开的文档的目录,该目录最有可能来自 Finder 的前窗口,但不一定。我喜欢编写无论如何都能工作的代码。

        on run {input, parameters}
            tell application "Finder"
                set currentPath to insertion location as text
                set x to POSIX path of currentPath
                display dialog "currentPath: " & (x as text)
            end tell
            return x
        end run
        

        我写了一个完整的“运行 AppleScript”动作来把事情放到上下文中:

        on run {input, parameters}
        
            # count the number of files
            set numFiles to 0
            repeat with f in input
                # warn the user that folders are not processed in this app
                tell application "Finder"
                    if (kind of f is "Folder") then
                        display dialog "The item: " & (f as text) & " is a folder.  Only files are allowed. Do you want to continue processing files or do you want to cancel?"
                    else
                        set numFiles to numFiles + 1
                    end if
                end tell
            end repeat
        
            # require that at least one file is being opened
            if numFiles < 1 then
                display alert "Error: the application Test1.app cannot be run because it requires at least one file as input"
                error number -128
            end if
        
            # get the current directory from the first file
            set theFirstFile to (item 1 of input)
            tell application "System Events" to set theFolder to (container of theFirstFile)
        
            # ask the user for a file name
            set thefilename to text returned of (display dialog "Create file named:" default answer "filename")
        
            # create the file
            tell application "System Events" to set thefullpath to (POSIX path of theFolder) & "/" & thefilename
            set theCommand to "touch \"" & thefullpath & "\""
            do shell script theCommand
        
            # return the input as the output
            return input
        
        end run
        

        “触摸”命令没问题。如果文件不存在,则创建它,如果存在,则仅更改修改日期(这还不错),但不会覆盖文件。如果您的文件被覆盖,则不是 touch 命令在执行此操作。

        我更改了默认文件名以删除扩展名“.txt” 此扩展名可能默认由 TextEdit.app 打开,但您可以在 Finder 中更改此设置,方法是为文件选择“获取信息”并更改“打开方式”属性。您可以更改哪个应用程序打开具有该扩展名的文件,也可以全部更改。例如,我所有的“.txt”文件都是用 BBEdit.app 打开的

        你会给我的答案投票吗?

        【讨论】:

          【解决方案4】:

          检查文件是否存在(假设已按照参考问题设置完整路径):

          tell application "Finder"
             if exists POSIX file thefullpath then
                  --do something here like
                  display alert "Warning: the file already exists"
             end if
          end tell
          

          不确定第二部分是什么意思,但如果你想连接存储在 var1 和 var2 中的字符串,你可以简单地做

          var1 & var2
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-11-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-23
            • 1970-01-01
            • 2019-11-01
            • 2021-01-03
            相关资源
            最近更新 更多