Monday, July 16, 2018

#1 2013-06-29 03:43:46 am

Eggers
Member
Registered: 2012-05-28
Posts: 21

Automator - Can Someone Make This Work?

I have a script that I'm trying to get working in Automator.  What I want to do is input a text file, run this script and it out put a new text file based on the changes the script was to do.  I can get this to run perfectly in Terminal but not anywhere else.  See script below:

while IFS= read -r || [[ -n "$REPLY" ]]
do
    printf '%s' "${REPLY:0:32}"
    printf '%s' "  Faux_Folder/"
    printf '%s\n' "${REPLY:33}"
done


If it needs to go into Applescript, that is completely fine to. Right now, I can only do it through terminal but I want to automate the process for others.

Thanks!

Offline

 

#2 2013-06-29 06:08:39 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Automator - Can Someone Make This Work?

To me it seems like your script is reading standard input. Please post the whole thing, (As you have implemented it so far in a Automator action, with parameters, and describe what it is supposed to do, that will make it easier to help you.

smile

Offline

 

#3 2013-07-01 06:41:15 pm

Eggers
Member
Registered: 2012-05-28
Posts: 21

Re: Automator - Can Someone Make This Work?

Hey!

So I open Automator, I just save a new file as a workflow right now.  Then this is what I start setting for the workflow:

1. Get Specified Finder Items (I select my text document here)
2. Run Shell Script:

while IFS= read -r || [[ -n "$REPLY" ]]
do
    printf '%s' "${REPLY:0:32}"
    printf '%s' "  Faux_Folder/"
    printf '%s\n' "${REPLY:33}"
done > NEW_FILE.txt


What this script is suppose to do is add custom text (eg Faux_Folder/) to every single line at the 32 character mark.

EG: Original text file is: 12345678912345678912345678912345/Testing/Testing
      New Text file will be: 12345678912345678912345678912345 Faux_Folder/Testing/Testing

Now, I get this to work in Terminal perfectly, just can't get it to work in Automator at all.  Would be nice to avoid using Terminal since a lot of my colleagues are not comfortable with using it.

Offline

 

#4 2013-07-01 10:06:56 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Automator - Can Someone Make This Work?

Hello.

I'll come back with a much better an generic way to do this. In this post. (It's kinda late in Norway right now.)

Offline

 

#5 2013-07-02 09:38:43 am

Eggers
Member
Registered: 2012-05-28
Posts: 21

Re: Automator - Can Someone Make This Work?

Totally ok! smile

Any help is much appreciated!

Offline

 

#6 2013-07-02 11:01:58 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Automator - Can Someone Make This Work?

Hello. I have created an Applescript Action for you, that is to be run from a Service/Workflow, and takes a selected file from the frontmost Finder window as its input.

it doesn't look like much that you presented to me, but that is because the filenames are then delimited by ":" and not "/" since the filename are represented in the hfs-format.

There is plenty with dialogs, and log statements, that shows you intermediate results of the conversion, if something is wrong, or there is something you want to ask about, then just ask!

Applescript:

on run {input, parameters}
   log input
   set filename to input as text
   tell (a reference to AppleScript's text item delimiters)
       set {oldtids, contents} to {contents, ":"}
       set filename to text items -3 thru -1 of filename as text
       set contents to oldtids
   end tell
   log "-Z" & filename
   set d to length of filename
   tell current application
       activate
       display dialog filename
   end tell
   
   if length of filename > 32 then
       set a to text 1 thru 31 of filename
       set b to text 33 thru -1 of filename
       set c to a & " Faux Folder" & b
   end if
   tell current application
       activate
       display dialog c
   end tell
   (* Your script goes here *)
   set input to filename
   return input
end run

Offline

 

#7 2013-07-02 11:55:58 am

Eggers
Member
Registered: 2012-05-28
Posts: 21

Re: Automator - Can Someone Make This Work?

my apologies. I just re-read my post and that I said "Text File" when I should have have "Text IN File".

The script I posted above changes the Text within a text file, not just the name of the file.  Basically, my text file will have several lines of text that I will need to add text to each line. Example Below:


12345678912345678912345678912345  Testing/Testing
12345678912345678912345678912345  Testing/Testing
12345678912345678912345678912345  Testing/Testing
12345678912345678912345678912345  Testing/Testing
12345678912345678912345678912345  Testing/Testing


and I would like it to add text now at the 34th character while keeping all the text. Example Below:

12345678912345678912345678912345  TEXT_HERE/Testing/Testing
12345678912345678912345678912345  TEXT_HERE/Testing/Testing
12345678912345678912345678912345  TEXT_HERE/Testing/Testing
12345678912345678912345678912345  TEXT_HERE/Testing/Testing
12345678912345678912345678912345  TEXT_HERE/Testing/Testing


Colleague changed some things around so here is now the updated script. It is still the same, just the character points have changed.

while IFS= read -r || [[ -n "$REPLY" ]]
do
    printf '%s' "${REPLY:0:34}"
    printf '%s' "Faux_Folder/"
    printf '%s\n' "${REPLY:34}"
done > NEW_FILE.txt

Offline

 

#8 2013-07-02 01:10:15 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Automator - Can Someone Make This Work?

Hello.

Ok, so I made a new service, that took text from a program (selected text). I checked off the the "Replaces selected" text option. I added a shell script action, which I let the standard Send selected text to stdin or whatever option be there.

I added a conversion of returns into linefeeds in your script, and now it works, when it is run, it replaces the selected text with the new one. I wouldn't think of creating new text files from within a Service. Then you'd rather use AppleScript, and a dedicated program as context for selecting the lines.

Well, here is your shell script:

tr '\r''\n' |while IFS= read -r || [[ -n "$REPLY" ]]
do
    printf
'%s'"${REPLY:0:34}"
    printf
'%s'"Faux_Folder/"
    printf
'%s\n'"${REPLY:34}"done

Offline

 

#9 2013-07-02 05:22:43 pm

Eggers
Member
Registered: 2012-05-28
Posts: 21

Re: Automator - Can Someone Make This Work?

Hmmmm...

Only unfortunate thing is that we need to change the custom text each time we use it so a service won't work. Unless it prompts a text field to input custom text? (eg Faux_Folder)

Offline

 

#10 2013-07-02 05:52:52 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Automator - Can Someone Make This Work?

You'll figure something out.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)