Applescript 如何将if/else与自动机循环结合使用

Applescript 如何将if/else与自动机循环结合使用,applescript,automator,Applescript,Automator,我已经自动化了一个标志的处理,但需要使其能够处理不同数量的标志。我在automator工作流中使用嵌套的applescript实现了这一点,但循环不会执行整个脚本 徽标被传递到传入文件夹,工作流/脚本更改桌面以指示系统处于活动状态,获取第一个文件,然后通过自动机操作继续格式化和分发文件。在结束时,循环返回到开始,直到传入文件夹为空 set FileAlias to (POSIX file "/Incoming") as alias tell application "Finder" c

我已经自动化了一个标志的处理,但需要使其能够处理不同数量的标志。我在automator工作流中使用嵌套的applescript实现了这一点,但循环不会执行整个脚本

徽标被传递到传入文件夹,工作流/脚本更改桌面以指示系统处于活动状态,获取第一个文件,然后通过自动机操作继续格式化和分发文件。在结束时,循环返回到开始,直到传入文件夹为空

set FileAlias to (POSIX file "/Incoming") as alias

tell application "Finder"
    count files of entire contents of FileAlias
    if the result is 0 then

        quit application "_Logos"

        launch application "Reset_Desktop"
    else

        move first item of FileAlias to (POSIX file "/Scripts")

    end if
end tell

正如您在脚本中看到的,如果传入文件夹为空,我将要求进程停止。这是可行的,但是重置桌面的附加步骤失败了。虽然如果我在文件夹中没有文件的情况下运行该过程,桌面会重置。

很难知道我的解决方案是否适用于您,因为我的系统上没有您提到的两个应用程序来正确测试您的代码。另外,在AppleScript代码之前和之后,不知道您的自动机操作是什么,也会使您很难提供问题的精确解决方案

也许这就是你想要的

set fileAlias to (POSIX file "/Incoming") as alias

tell application "Finder"
    set fileCount to count files of entire contents of fileAlias

    repeat until fileCount is 0
        move first item of fileAlias to (POSIX file "/Scripts")
        delay 0.1
        set fileCount to fileCount - 1
        if fileCount is 0 then
            launch application "Reset_Desktop"
            quit application "_Logos"
        end if
    end repeat
end tell

我想出来了。上面的回答引导我以不同的顺序写下事件。我还将Applescript一分为二;一个出现在工作流的开始,另一个出现在循环操作之前的末尾

第一个Applescript:

    set FileAlias to (POSIX file "/Incoming") as alias   
    tell application "Finder"
        count files of entire contents of FileAlias
        if the result is 0 then
            null
        else
        move first item of FileAlias to (POSIX file "/Scripts") 
    end if
end tell
set FileAlias to (POSIX file "/Incoming") as alias    
tell application "Finder"       
    count files of entire contents of FileAlias
    if the result is 0 then         
        launch application "Reset_Desktop"
        quit application "Logos"        
    end if      
end tell
第二个Applescript:

    set FileAlias to (POSIX file "/Incoming") as alias   
    tell application "Finder"
        count files of entire contents of FileAlias
        if the result is 0 then
            null
        else
        move first item of FileAlias to (POSIX file "/Scripts") 
    end if
end tell
set FileAlias to (POSIX file "/Incoming") as alias    
tell application "Finder"       
    count files of entire contents of FileAlias
    if the result is 0 then         
        launch application "Reset_Desktop"
        quit application "Logos"        
    end if      
end tell

“很难知道我的解决方案是否适用于您,因为我没有这两个应用程序”脚本中每个应用程序都被精确引用一次:一个被命令启动
;另一个要退出
。你是说你不知道
启动
退出
如何工作?很遗憾,这个答案没有你对另一个我非常喜欢的问题的答案的所有优点:OP的脚本有很多新手犯下的陷阱,你错过了一个对它进行改进的机会,或者至少指出了它潜在的缺陷。此外,如果文件夹
/Incoming
中有包含任何项目的子文件夹,则此脚本将引发错误。我会让你去弄清楚这背后的原因。另外,如果fileCount为0,那么在条件为
的循环中设置
是否有意义,直到fileCount为0为止?想象一下,如果你移植了<代码>如果块,坐在重复循环之外,然后考虑重复循环中剩下的内容;然后考虑重复循环正在做什么,以及你是否真的需要一个重复循环来做到这一点。我不清楚,如果<代码>退出应用程序“SoLogo”< /C> >是当前的AutoCAD工作流应用程序的名称,或者是完全不同的应用程序。然而,对我来说,
launch
显然是指一个单独的应用程序。考虑到他使用
quit
命令可能是试图退出实际的自动机工作流,我认为更改命令顺序可以解决其中一个问题。更改命令顺序完全可以。我对此没有任何异议。我承认并同意OP在他的问题中留下了很多模棱两可的地方,没有提供必要的元素来给出一个好的答案。然而,所有在我脑海中引起悲伤微笑的问题都与问题的上下文无关,只与您作为一段代码编写的脚本有关。对于OP未能提供必要信息的情况,我的建议是留下评论,要求他们澄清,并提供缺失的元素。您将能够提供更多帮助。@wch1zpink再次感谢您帮助我解决这个问题。由于我缺乏细节,我发现有些混乱,我将尝试为您和CJK澄清。在AppleScript中创建和命名变量时,有两种首选方法。。。下划线样式(例如,
下划线_命名
)和驼峰大小写命名约定(
驼峰大小写命名
)。这就是为什么我在回答中使用了
set fileAlias
而不是
set fileAlias
。请问文件夹
“Incoming”
“Scripts”
的完整文件路径是什么?从脚本来看,文件夹似乎位于硬盘驱动器的顶级文件夹中。没有理由需要在这里创建文件夹,通常也不建议这样做。您可能希望考虑将它们移到您的用户主目录,其中路径将是“代码>”/用户/ %%/%/传入/“代码>和<代码>”/用户/ %%%/脚本/ <代码>。