Help appending file names?

Discussion in 'Mac Programming' started by tobefirst, Feb 12, 2014.

  1. tobefirst macrumors 68040

    tobefirst

    Joined:
    Jan 24, 2005
    Location:
    St. Louis, MO
    #1
    Here's the situation:

    I have .pdf files of all the separate jobs that I've done. Each of these .pdfs are in separate job folders. All the job folders are within the Documents folder on my computer. The job folders are all marked (via spotlight comments) with a job number (e.g., 3-1234). This same number appears somewhere on the corresponding .pdf.

    What I would like to do is find an automated way to find and copy all of these .pdfs to a single new folder called something like "all my jobs" where my coworkers, on PCs, can search for the job number and find the corresponding .pdf.

    I dropped one of my .pdfs into a shared location and asked a coworker if searching for the job number would return the correct .pdf. Unfortunately, it did not. It seems whatever version of Windows the organization is using isn't searching inside files for content. Either that or I need to instruct him on how to search for this file in a way that will find it.

    Or, perhaps more likely…

    I need a way to take the job number that is in the .pdf (and marked on the enclosing folder) and append that job number to the .pdf filename as it copies it to the new folder.

    Does that make sense?

    I was able to create an Automator application that would do the following
    • Find Finder Items
    - Any content contains "3-"
    - Kind is PDF
    • Copy Finder Items
    - to "all my jobs" folder

    …but I'm not sure how I would be able to do the appending. Any ideas of a workflow or a separate application that could accomplish this?
     
  2. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #2
    I think you can execute bash script in those workflows. If this is correct then execute a bash script that greps the file for the 3-job number or have that set as a variable it passes to the bash script when it executes a cp /path/to/my\ jobs\ folder/file-job\ number\ variable.pdf on each file as it is processed by the workflow.
     
  3. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #3
    I think the app Hazel can be configured to do what you want.
     
  4. kryten2, Feb 12, 2014
    Last edited: Feb 12, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    You can replace your Copy Finder Items action with a Run Applescript action and replace the default code with this :

    Code:
    on run {input, parameters}
    	set myDest to (path to documents folder as string) & "All my jobs:"
    	repeat with aFile in input
    		set baseName to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of (aFile as string)
    		tell application "Finder"
    			set containerComments to comment of aFile's container
    			set duplicateFile to duplicate aFile to myDest
    			set duplicateFile's name to (baseName & "-" & containerComments & "." & aFile's name extension)
    		end tell
    	end repeat
    end run
    Note : As you can see it expects your "All my jobs" folder to be located in your Documents folder. If that's not the case change the myDest line to where your "All my jobs" folder is located in the form of a Mac path eg.
    "nameofdisk:folder:folder:All my jobs:" or if it's located somewhere in some folder in your home folder "nameofdisk:Users:yourusername:folder:All my jobs:". It will append the Spotlight comments of the file's container.
     

    Attached Files:

  5. tobefirst thread starter macrumors 68040

    tobefirst

    Joined:
    Jan 24, 2005
    Location:
    St. Louis, MO
    #5
    Holy cow! Thanks a lot! That's so helpful. I tried inserting the code you gave me, but when I run it, Automator gives me this error:

    The action “Run AppleScript” encountered an error.
    Check the actionʼs properties and try running the workflow again.


    If I try to just run the Applescript, it gives me this error:

    Syntax Error
    Can’t make «class ctnr» of item 1 into type reference.


    I had edited the location of the folder to what I thought it should be, but perhaps I'm doing it wrong. The drive I'm trying to save to is a external drive on our (Mac) server that is shared with me (i.e. I have read/write access). Here is what I put into the Applescript you wrote:

    set myDest to (path to documents folder as string) & "ExternalDriveName:EnclosingFolder1:EnclosingFolder2:EnclosingFolder3:EnclosingFolder4:Allmyjobs:"

    …but perhaps there is something else I need to add if it is an external drive that is mounted on another computer and shared with me?

    Here is the address of that external drive that I get by using "Get Info" on the drive.

    afp://10.9.30.30/ExternalDriveName

    Would you mind helping me edit the Applescript? You've been wonderfully helpful so far. Thanks so much!

    ----------

    Thanks for the suggestion. I've reached out to the developers to see if what I'm looking for is something their software can accomplish.

    ----------

    Thanks for the help. It's a bit over my head, unfortunately. It seems someone else has me on the right track. I appreciate your effort.
     
  6. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #6
    I mentioned it because from their "what's new" section (below) it looks like that file renaming section would cover what you are after. I see they have a free 14 day trial.

    [​IMG]
     
  7. tobefirst thread starter macrumors 68040

    tobefirst

    Joined:
    Jan 24, 2005
    Location:
    St. Louis, MO
    #7
    Glad you did. If I don't hear back from the developers, or if I can't get the Automator/Applescript to work right (with a bit of help), I will be downloading the trial and playing around. Thanks again!
     
  8. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    Sure. As you have noticed the set myDest to "" isn't correct in your case. The best way to get your correct path to your folder would be to launch the Appescript Editor application and paste in the following :

    Code:
    choose folder
    Click the Run button and in the Choose a Folder window that pops up navigate to your Allmyjobs folder on your server. Look at the result pane (see thumbnail) to see the path it returned in the form of an alias. That's the path you need.

    In my example I used a shared folder "All my jobs" I shared via AFP on my Mac mini. The result I got when running choose folder was this :

    Code:
    alias "All my jobs:Folder 1:Folder 2:Folder 3:Allmyjobs:"
    As you can see I used "All my jobs:Folder 1:Folder 2:Folder 3:Allmyjobs:" but you can also use "MavericksDP:Volumes:All my jobs:Folder 1:Folder 2:Folder 3:Allmyjobs:" where MavericksDP is the name of my local hard disk.(e.g. the name of the disk of the mac from where you are running your workflow) Yours will probably be different so if you want to use that form you will have to change it!

    Note : I hope it's a little clearer now. There is no error trapping in the script meaning it assumes the server is up and the folder is available. I'll try to make it more robust when I have some time to spare.
     

    Attached Files:

  9. tobefirst thread starter macrumors 68040

    tobefirst

    Joined:
    Jan 24, 2005
    Location:
    St. Louis, MO
    #9
    Thanks again for your help. It still isn't working. I'm still getting the "Syntax Error/Can’t make «class ctnr» of item 1 into type reference" message.

    I've uploaded what my Automator app looks like now. (The Get Specified Finder Items part Automator told me needed to be there while I build the app, but once I finish, I believe I can delete it before saving.) You can see exactly the file structure I got when I ran "choose folder" in the Applescript editor.

    Perhaps one reason it isn't working right is because I'm accessing the shared drive differently than you? I'm not sure what the terminology is for how I'm accessing it– I've asked someone and will update this post with the proper terminology. I have included a screenshot of how my Finder looks. I mount the drive by going to Go:Connect to Server in Finder and it shows up under Devices instead of Shared in my Finder Sidebar, though the address has AFP in it...

    Does this change anything at all?

    I REALLY REALLY appreciate the help. It'll make some things around my office easier if I can get this to automate. HOWEVER...Feel absolutely free to withdraw helping if/whenever you'd like. I don't expect you to help me endlessly with this. Thanks.
     

    Attached Files:

  10. kryten2, Feb 13, 2014
    Last edited: Feb 13, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    1. Is this a service? The first action is clearly receiving input from something. You don't need the Get specified finder items action. Start with a simple workflow in Automator with 2 actions. The Find Finder items and the Run Applescript action.
    2. Please remove the (path to documents folder as string) & part from the set myDest line!!!!!
    3. I don't see any output or tumbnail from the "choose folder" in the Applescript editor in your post.
    4. Please do not forget to do step 2 in the list. You absolutely need to have the correct path to that folder.
    5. Start with a workflow first that way you can run it from within Automator. If everything works as it should you can then save it as an application!
    6. I know I'm starting to sound a bit like a drag but if it's still not clear by now that you need to remove the (path to documents folder as string) & part from the set myDest line then you can also remove the line completely and replace it with this :
      Code:
      set myDest to (choose folder) as string

    Looking at you tumbnails i think this would be the correct path :

    Code:
    set myDest to "WGS_DataFiles:WGS_DataFiles:WGS_Shared:Graphics:Chris:pdfs:"
     
  11. tobefirst thread starter macrumors 68040

    tobefirst

    Joined:
    Jan 24, 2005
    Location:
    St. Louis, MO
    #11
    Bingo! THANK YOU SO MUCH! It is running perfectly now! Sorry if I was thickheaded. Again, many, many, many thanks!
     
  12. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #12
    You're welcome.
     

Share This Page