Automator “copy finder object” with AppleScript

让人想犯罪 __ 提交于 2021-01-29 09:26:46

问题


I have created an Apple Automator service that

  • receives PDFs
  • encrypts them
  • copies the encrypted PDF to a destination folder
  • renames the encrypted PDF

These are all commands from the Automator. However, the copying as an Automator command can only copy to a pre-defined folder.

I would like to control this part by an AppleScript which reads out the user name and selects a folder accordingly:

on run {input, parameters}
    
    set user_script to "echo $USER"
    set username to do shell script user_script
    
    if username = "A" then
        set standardpfad to "/Users/" & username & "/whatever"
    else if username = "B" then
        set standardpfad to "/Users/" & username & "/foo"
    else
        display dialog "I don't know this user!" with title "ERROR" buttons {"OK"} default button "OK"
        return
    end if
    
    #actual copying
                
end run

Unfortunately, I don't know how to handle the input in way that it resembles the "Copy Finder object" command in Automator. Can anyone please help me?

Thank you!

Edit: Automator screenshot


回答1:


Going by your original script, which seems to want to move all the files to the user's home folder, you can accomplish what you want using an automator variable. First, go to the upper left of the Automator window, click on the tab button that says 'Variables', then click on the 'Locations' item. Look for the item that says 'Home' (I believe that's 'Privat' on your machine's language):

This provides to a path to the user's home folder, for whichever user is running the workflow (system and machine independent). Drag this variable over to the Copy Finder Items (Finder-objekte kopieren) action, and drop it on the 'To:' ('Nach:') pull-down menu. It should look like this:

That should do the trick.

There are an assortment of system defined user paths you can choose from. You can also define a custom one using the special 'Text' variable (under 'Text & Data'), typing a path in standard unix notation where the tilde ('~') represents the user's home folder: e.g., ~/path/to/Custom Folder/.

If you're doing something more complicated and really need to use a Run AppleScript action, all you need to know is that the list of files is passed into the action in the input variable as a list of aliases, and whatever you return (should be a list of aliases or posix paths), will be passed on to the next action. For example:

on run {input, parameters}
    set output to {}
    repeat with this_item in input
        set new_item to this_item -- ... obviously you'd do something other than just copy
        copy new_item to end of output
    end repeat
    
    return output
end run

But it doesn't seem like you need to do that here; the special Automator variables should get you where you're going.

EDIT

Per comments, here's a revised version of the workflow...

Add the following actions to the workflow given in the question in place of the "Copy Finder Objects" action. Note that the second and sixth actions are set to ignore input from the previous action. These actions do the following:

  1. Save the list of files to be copied to a storage variable called 'FileToCopy'; pass no data on
  2. Get the path to the user's home folder; pass it to the next action
  3. Get the user's 'user name'; pass the home folder and user name to the next action as a list
  4. Run an AppleScript that constructs a unix path string from the input list; pass the completed path string on to the next action
  5. Save the path string into a variable called 'DestinationFolder'; pass no data on
  6. Retrieves the list of files to copy we saved in step #1; pass it on to the next action
  7. Copy the files to the selected folder, using the 'DestinationFolder' variable we saved in step #5; pass these on to the Rename Finder Items action (not shown here)

Give it a go, and let me know how it works.



来源:https://stackoverflow.com/questions/63129932/automator-copy-finder-object-with-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!