You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

适配projCode长度的文件夹前缀重命名脚本问题求助

AppleScript文件夹重命名脚本适配问题解决

你的代码无法运行是因为AppleScript获取字符串长度的语法错误,不能用projCode count这种写法,正确的方式是使用length of projCode或者count of projCode。

修正后的完整代码

-- 获取projCode的长度
set projCodeLength to length of projCode
set prePend to ""

-- 根据长度判断前缀取值逻辑
if projCodeLength = 11 then
    set prePend to text -6 thru -1 of projCode
else if projCodeLength = 10 then
    set prePend to text -5 thru -1 of projCode
end if

-- 执行文件夹重命名
tell application "System Events" to set name of projectFolder to (prePend & " - " & (get name of projectFolder))

代码说明

  1. 先将字符串长度存入变量projCodeLength,避免重复计算,提升代码可读性
  2. 修正了长度判断的语法错误,AppleScript中必须通过length of <字符串>或count of <字符串>获取长度
  3. 提前初始化prePend变量,防止后续未进入分支时变量未定义的问题

简化写法(可选)

如果想让代码更简洁,可以用一行逻辑完成前缀取值:

set prePend to text (-6 + (length of projCode = 10)) thru -1 of projCode
tell application "System Events" to set name of projectFolder to (prePend & " - " & (get name of projectFolder))

原理:AppleScript中布尔值true等价于1,false等价于0。当projCode是10位时,length of projCode = 10返回true,计算后起始位置为-6 +1 = -5(取最后5位);11位时返回false,起始位置为-6(取最后6位),刚好适配两种场景。

内容的提问来源于stack exchange,提问作者Vicente MartinezJr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 01:02:47