【问题标题】:Filename increment in applescriptapplescript中的文件名增量
【发布时间】:2015-12-28 20:51:19
【问题描述】:

我正在使用轻量级自动化进程 (applescript) 来检测像“file_v01”、“document_v03”这样的文件名都以“_vXX”结尾的文件名要递增(到“file_v02”,而且这方面的内容少得惊人。

我尝试简单地检测并删除文件名中的最后两个字符,但无济于事,任何想法都会很棒。不需要花哨,只要 _v02 变成 _v03。

任何帮助都会很棒!

【问题讨论】:

    标签: file applescript increment automator


    【解决方案1】:

    这是您想要的脚本。

    您需要先设置文件名和计数器之间的分隔符类型。然后你需要定义你的计数器应该有多少位数。

    我假设您的文件名中的计数器后面可能有一些文本(如文件扩展名!):

    set Separator to "_v" -- separator between name and counter
    set DigitsCount to 2 -- assuming only 2 digits for counter like 01, 02, ...99
    
    set myName to "testxxx_v07_Copy.txt" -- example for the test
    
    set PosSep to offset of Separator in myName
        if PosSep is 0 then
    -- no separator, then no counter..do what's required !
    else
    set BaseName to text 1 thru (PosSep - 1) of myName
    set myCount to (text from (PosSep + (length of Separator)) to (PosSep + (length of Separator) + DigitsCount - 1) of myName) as integer
    set PostName to text from (PosSep + (length of Separator) + DigitsCount) to -1 of myName
    end if
    -- convert the counter to +1 in string with '0' before
    set newCount to "00000" & (myCount + 1) as string
    set newCount to text from ((length of newCount) - DigitsCount + 1) to -1 of newCount
    set NextName to BaseName & Separator & newCount & PostName
    
    -- BaseName now contains the basic name before the counter
    -- newCount contains the new counter value (+1) as string formatted with x digits
    -- PostName contains the current value of myName after the counter
    -- NextName contains your new file name with new counter and extension if any
    

    【讨论】:

      【解决方案2】:

      这是AutomatorAppleScript,它会增加文件名的最后两个字符

      on run {input, parameters}
          tell application "System Events"
              repeat with thisFile in input
                  set {tName, nameExt} to {name, name extension} of thisFile
                  if nameExt is not "" then set nameExt to "." & nameExt
                  set newName to my incrementNumber(tName, nameExt)
                  if newName is not "" then -- the last two characters is a number
                      set name of thisFile to newName
                  end if
              end repeat
          end tell
          return input
      end run
      
      on incrementNumber(n, e) -- name and name extension
          set tBase to text 1 thru ((length of n) - (length of e)) of n -- get the name without extension
          try
              set thisNumber to (text -2 thru -1 of tBase) + 1 -- get the last two characters and increment
              if thisNumber < 10 then set thisNumber to "0" & thisNumber -- zero padding
              set tBase to text 1 thru -3 of tBase -- remove the last two characters
              return tBase & thisNumber & e -- the updated name (the basename without the last two characters + the number + the extension)
          end try
          return "" --  the last two characters is not a number
      end incrementNumber
      

      【讨论】:

      • 这太棒了!但是,我可以删除对话框以“查找文件”,它只会增加文件自动机作为传入连接的任何内容吗?
      • 我更新了脚本以在 Automator 工作流程中运行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      相关资源
      最近更新 更多