Applescript to open various files and add info from filename to the file

Hi guys,
first time using this forum...
and first time trying to do a script for apple...
I figured out how to do simple things... like open files and stuff to text files...  but don't know where to begin:
I have a directory datalogs...
within the directory i have multiple text files that have the following format:
dataXXYYY_Waf2_7_19_11.csv
dataXXYYY_Waf3_7_19_13.csv
dataXXYYY_Waf25_7_19_16.csv
typically 25 files, but sometimes less
I would like to be able to take each file within the datalogs folder ...
Scan the file name and look for the number afer Waf
The number can be anywhere from (single digit) 1 to 25
I would like to take that value after Waf  and then append a line in the txt file
the line I would have to add will be 5 rows down and it shoulld say:
wafer,1 
the 1 above is the value that was in the file name....
i.e.
for the file
dataXXYYY_Waf25_7_19_16.csv
before append the file contains random data ... i.e.
dummyline1
dummyline2
dummyline3
dummyline4
dummyline5
dummyline6
Then after the script...
the file will be saved with same name:
dataXXYYY_Waf25_7_19_16.csv
and the data should look like:
dummyline1
dummyline2
dummyline3
dummyline4
wafer,25
dummyline5
dummyline6
any help will be appreciated
-thanks

Copy your source folder - named 'datalogs' to your home directory and run this script by copying into a new AppleScript Editor document and pressing Run. 
I think I've followed your specification, and there will be other ways to do this, but ask if you have questions.  (I have put comments in the code which should explain some of it!)
There is no error checking, but I have made copies of the original files in the source directory.  (You can comment out the line "duplicate theFile" by prefixing with "--" when you are sure it's working.
As always, ensure your data is backed-up before proceeding.
--starts here
--sets path to datalogs folder
set theDirectory to (path to home folder as text) & "datalogs"
set fileNames to {}
tell application "Finder"
  --gets all the files in the source directory
          set fileList to files of folder theDirectory
  --iterates through the files
          repeat with theFile in fileList
  --makes a duplicate
  duplicate theFile
  --puts the fileName and fileLocation into records in a list of lists
                    set end of fileNames to {fileName:name of theFile, fileLoc:theFile as alias}
          end repeat
  --iterates thru the files
          repeat with theFile in fileNames
  --gets offset of the string "Waf"
                    set WafOffset to offset of "Waf" in (fileName of theFile)
  --trims the name so that it now starts with the character after "Waf"
                    set fileName to text (WafOffset + 3) thru end of fileName of theFile
  --gets offset of the next underscore
                    set UScoreOffset to offset of "_" in fileName
  --stores the desired number by trimming off the end of the file name
                    set fileNumber to text 1 thru (UScoreOffset - 1) of fileName
  --sets up the text to insert by prefixing with "Wafer," and suffixing with return
                    set insertText to "Wafer," & fileNumber & character id 10
  --opens the file with write permission
                    set FileRef to open for access (fileLoc of theFile) with write permission
  --initialise container
                    set fileContents to ""
  --puts first five lines into container
                    repeat 5 times
                              set fileContents to fileContents & (read FileRef until character id 10)
                    end repeat
  --adds the new text to the container
                    set fileContents to fileContents & insertText
  --adds the rest of the file to the container
                    set fileContents to fileContents & (read FileRef)
  --effectively empties the file
  set eof FileRef to 0
  --writes contents of the container to the file
  write fileContents to FileRef
  --closes the file
  close access FileRef
          end repeat
end tell
--ends here

Similar Messages

  • Is there a way to save an image file as a jpeg and add -web or -print to the file name?

    I am trying to find a way to save my image file as a jpeg and add -web or -print to the existing file name.  This way I can easily see which files are lower resolution for web posting or high resolution for printing.

    Jason,
    I helped somebody do a very similar thing about a week ago.
    The trick is to use the Batch command's ability to construct a file name.
    Tell the batch to append, e.g. "-x" without quotes, to the name of each saved file.
    The batch runs an Action which does a save as JPEG, resize image, add logo, save as JPEG, then close document.
    The output is "<document name>-x.jpg" and "<document name>-x-x.jpeg".
    Then use Windows Explorer to rename "*-x-x.jpg" to "*-print.jpg".
    Then rename "*-x.jpg" to "*-web.jpg".
    See http://forums.adobe.com/thread/1038992?start=32
    The name construction that I used there was just an "x" appended to the document name. Use whatever you like. Also juliew subsequently referred to the method as a "double-batch" run, although it is not. One run of the batch will output two JPEGs for each input document.
    I hope the disagreements in that thread don't cause too much disruption.
    If you want, the Batch command itself can be recorded in another Action which would store the parameters of the batch, then you could run that Action anytime without having to set up the details of the batch each time. It would always output to one specific folder, though.

  • I have sent a file to the trash bin. When I come to empty the trash bin everything goes out except this one particular file and I get a note saying the file is 'in use' but it is not. How can I delete this file from trash?

    I have a Macbook Pro with OS X Lion 10.7.2. I have sent a work file to the trash bin along with many other files. When I empty the trash this one particular file remains and I get a message that this file is still in use. I have tried returning the file to my documents, opening it, closing it and then sending it to trash and again I try to empty trash but the same message comes up - Q - How can I trash this file permanently?

    First just try logging out and back in.  Whatever is holding on to it may not be running after the logout thus freeing it so you can trash it after logging back in.
    You might also try Trash It!
    Lastly, look at the following for suggestions:
    The X Lab: Solving Trash Problems

  • I've exported my pages graphics document as a PDF file and can't seem to reduce the file size without compromising the quality of the photo's used even after choosing "best" quality.  I'm trying to send the file via e-mail but it's too big.  ??????

    I am trying to e-mail a graphics pages document I made as a PDF file.  After trying to resize the file, the photo's quality is compromised (really blurry) even after choosing "Best" quality.  What am I doing wrong.  As a regular full sized file (PDF) it's fine, but when it gets reduced, it's bad and it needs to get printed from the e-mail copy I'm trying to send.  HELP! Please.

    If there are a lot and/or large images the file will inevitably be large, no matter what.
    You can reduce them down as far as possible by cropping and scaling them to the final size before creating the pdf. You can do this by opening the images in Preview .app and using Menu > Tools to reduce the resolution and to crop.
    If you have used transparency effects eg shadow, reflection, transparency, 3D charts, OSX will by default render these at 72dpi in the pdf.
    Peter

  • Selecting a list of files and creating a list of only the file names?

    Hopefully this is an easy question, one that I can't seem to figure out. I have a folder of photos and I would like to select all their file names and create a text list of just the file names. Every time I try I select the photos themselves and when pasted into my text document all of a sudden I have a lot of images. Any advice on how to do this? Thanks Jeff

    You're part way there. Do this in TextEdit. Open a new document, CMDSHIFTT to put it into plain text format, paste in the contents of your photo folder, and be surprised when only the names get pasted and not the images. If you want them in alphabetical order, change the photo folders view to list and click on the name column before copying.

  • How to get contacts and calendar info from my old palm files

    I used to use a Palm Tungsten T3. I no longer have it but until recently I still used the Palm Desktop. My computer "died" and I was able to copy all the Palm files of of it but now I have no idea how to get the contact and calendar(less important than contacts) info in some form I can use. I do not currently have any Palm software installed on my computer. I have Microsoft Windows XP on my computer now and use Outlook or Outlook Express. Does anyone have any advice for me. Thanks in advance.
    Post relates to: Tungsten T3

    I'm having the same problem.  Desktop 4.1.4 on old PC.  New PC with Win 7.   Loaded 6.2 and when I went to Import 6.2 is looking for .aba files.  Old PC has all .dat files.  How do I import or transfer addresses and other contact info to new PC??  Thanks.

  • My desktop files and folders no longer appear on the screen. I think this is a result of an iPhoto updating.  It appears (from getting info) that now Desktop is being treated like a normal file for which an application to open it must be supplied.  Help?

    My desktop files and folders no longer appear on the screen.  I think this is a result of an iPhoto updating.  It appears (from getting info) that now Desktop is being treated like a normal file for which an application to open it must be supplied.  Can anything be done?  Incila

    You are not going to be able to run your old system from the backup on this old computer as the hardware is incompatible.
    You need to get a new computer or a refurbished one.

  • [svn:bz-trunk] 10059: update two package-info.java files, and add one new one.

    Revision: 10059
    Author:   [email protected]
    Date:     2009-09-08 11:31:48 -0700 (Tue, 08 Sep 2009)
    Log Message:
    update two package-info.java files, and add one new one.
    Modified Paths:
        blazeds/trunk/modules/proxy/src/flex/messaging/services/http/proxy/package-info.java
    Added Paths:
        blazeds/trunk/modules/core/src/flex/messaging/util/concurrent/package-info.java
        blazeds/trunk/modules/core/src/flex/messaging/validators/package-info.java

    Revision: 10059
    Author:   [email protected]
    Date:     2009-09-08 11:31:48 -0700 (Tue, 08 Sep 2009)
    Log Message:
    update two package-info.java files, and add one new one.
    Modified Paths:
        blazeds/trunk/modules/proxy/src/flex/messaging/services/http/proxy/package-info.java
    Added Paths:
        blazeds/trunk/modules/core/src/flex/messaging/util/concurrent/package-info.java
        blazeds/trunk/modules/core/src/flex/messaging/validators/package-info.java

  • Finder issues and Get Info refusing to show the number of files on items on the main hard drive.

    I am having finder issues. Mostly on the main hard drive. It takes ages to find things and I get the spinning wheel every time I open a folder.
    So I moved all of my files to a external drive.  When I had finished transfering I hit get info on the files on the external drive and on the same folder on the main drive.  To ensure that it has copied over all the files.  Because it was acting up and not copying all the files.
    Get Info refuses to show the number of files within the folders on the main hard drive. It hasppily tells me everything on any of the external drives.
    I have reset, the imac.  I have re indexed 3 times.  I have deleted the finder plist files.  I have reset finder using terminal commands. I have moved nearly everything off the main hard drive. But nothing is working.
    Can anyone offer any suggestions as to what could be causing my finder and get info issues.
    Any help wpuld be appreciated.
    Running Mountain Lion on a late 2009 Imac

    Some of your user files (not system files) have incorrect permissions or are locked. This procedure will unlock those files and reset their ownership, permissions, and access controls to the default. If you've intentionally set special values for those attributes, they will be reverted. In that case, either stop here, or be prepared to recreate the settings if necessary. Do so only after verifying that those settings didn't cause the problem. If none of this is meaningful to you, you don't need to worry about it, but you do need to follow the instructions below.
    Back up all data.
    Step 1
    If you have more than one user, and the one in question is not an administrator, then go to Step 2.
    Enter the following command in the Terminal window in the same way as before (triple-click, copy, and paste):
    sudo find ~ $TMPDIR.. -exec chflags nouchg,nouappnd,noschg,nosappnd {} + -exec chown $UID {} + -exec chmod +rw {} + -exec chmod -N {} + -type d -exec chmod +x {} + 2>&-
    This time you'll be prompted for your login password, which won't be displayed when you type it. Type carefully and then press return. You may get a one-time warning to be careful. If you don’t have a login password, you’ll need to set one before you can run the command. If you see a message that your username "is not in the sudoers file," then you're not logged in as an administrator.
    The command may take several minutes to run, depending on how many files you have. Wait for a new line ending in a dollar sign ($) to appear, then quit Terminal.
    Step 2 (optional)
    Take this step only if you have trouble with Step 1, if you prefer not to take it, or if it doesn't solve the problem.
    Start up in Recovery mode. When the OS X Utilities screen appears, select
              Utilities ▹ Terminal
    from the menu bar. A Terminal window will open. In that window, type this:
    res
    Press the tab key. The partial command you typed will automatically be completed to this:
    resetpassword
    Press return. A Reset Password window will open. You’re not going to reset a password.
    Select your startup volume ("Macintosh HD," unless you gave it a different name) if not already selected.
    Select your username from the menu labeled Select the user account if not already selected.
    Under Reset Home Directory Permissions and ACLs, click the Reset button
    Select
               ▹ Restart
    from the menu bar.

  • Could not save Preferences because the file is locked, you do not have necessary access permissions, or another program is using the file. Use the 'Get Info' command in the Finder to ensure the file is unlocked and you have permission to access the file.

    I have this massage every time close ps cc
    Could not save Preferences because the file is locked, you do not have necessary access permissions, or another program is using the file. Use the ‘Get Info’ command in the Finder to ensure the file is unlocked and you have permission to access the file. If the problem persists, save the document to a different file or duplicate it in the Finder.

    You may get better help in Photoshop General Discussion
    The Cloud forum is not about using individual programs
    The Cloud forum is about the Cloud as a delivery & install process
    If you will start at the Forums Index https://forums.adobe.com/welcome
    You will be able to select a forum for the specific Adobe product(s) you use
    Click the "down arrow" symbol on the right (where it says All communities) to open the drop down list and scroll
    If FINDER means Mac, read below (and try to give more information when asking a question)
    Mac 10.9.3 workaround https://forums.adobe.com/thread/1489922
    Enable Mac Root User https://forums.adobe.com/thread/1156604
    -more Root User http://forums.adobe.com/thread/879931
    -and more root user http://forums.adobe.com/thread/940869?tstart=0

  • Photoshop open multiple image and add different texts

    Hi All,
    Hope you are all doing great. I just started doing photoshop scripting due to the requirement which I will mention in detail below.
    My scripting is knowledge is very basic so I was hoping you guys could be able to help me out with this.
    The requirment is that I have 100's of images which needs to be opened in photoshop and add text based on the image name then save the image as JPEG at 70 quality. For example:
    I have images named as below in my desktop folder,
    Image1.jpg
    Image2.jpg
    Image3.jpg
    Image4.jpg
    Image5.jpg
    Image6.jpg
    Image7.jpg
    Image8.jpg
    Image9.jpg
    Image10.jpg
    For the script, I think the easiest way is that if I define what image name are:
    image[1] = Image1.jpg
    image[2] = Image2.jpg
    image[3] = Image3.jpg
    image[4] = Image4.jpg
    image[5] = Image5.jpg
    image[6] = Image6.jpg
    image[7] = Image7.jpg
    image[8] = Image8.jpg
    image[9] = Image9.jpg
    image[10] = Image10.jpg
    and for text I have done this:
    text [1] = Image1
    text [2] = Image2
    text [3] = Image3
    text [4] = Image4
    text [5] = Image5
    text [6] = Image6
    text [7] = Image7
    text [8] = Image8
    text [9] = Image9
    text [10] = Image10
    So everytime when I run the script image1 will be added to the photoshop and the text name Image1 will be added (For this I think defining layer name would be better so I will gave "Imagetext").
    Then the image will be saved on a specific location on my desktop.
    I have all this plan on what I want to do but I don't know how to write the script using photoshop API and my limited javascript coding.
    So if you guys could help me out with this, that would be awesome. Hope to hear on this soon.
    Kind Regards,
    Zoogle

    Hi Ghoulfool,
    Thank you very much for taking your time in looking into my question.
    Also thanks for the script you've provided. Your script works perfectly close to my requirements but there are few minor addon that I would like to request please.
    My apologies for not being very clear
    First of all to answer your questions:
    Image size varies but mostly between 1200-1000 pixels.
    Good question regarding text size, I would like to control the size of the text based on how long the character length is.
    Position varies so I would like to adjust it manually or if I could definie within the script that would be superb.
    The images open in new window rather that staying in the currently opend psd, I have other layers which I need to keep it as static and use the script to add the image between these layers.
    I wouldn't mind if all these 100's of images stay in current psd as layers.
    I was hoping if possible I would have more control over what text I would like to put on the image.
    Below is the example , I am sure the code below is wrong but with my limited scripting that's all I could come up with
    This is the code that would be controlling what text/text size appear on images based on the i value:
    var publicPath = 'C:/Users/Username/Desktop/imagesavetest';
    var outputPath = publicPath;
    var imagetext = new Array();
    imagetext[0] = "imagetext0";
    imagetext[1] = "Apple";
    imagetext[2] = "imagetext2";
    imagetext[3] = "Pineapple";
    imagetext[4] = "Orange";
    imagetext[5] = "imagetext5";
    imagetext[6] = "imagetext6";
    imagetext[7] = "Berries";
    for (var i in imagetext) {
      textLength = imagetext[i].length;
      saveMyFile = imagetext;
      theText.contents = imagetext[i].toUpperCase();
      switch (true) {
        case textLength <= 9:
          theText.font = "Arial";
          theText.size = 38;
          theText.contents = imagetext[i].toUpperCase();
          break;
        case textLength > 9 && textLength <= 12:
          theText.font = "Arial";
          theText.size = 28;
          theText.contents = imagetext[i].toUpperCase();
          break;
      saveAsJPEG(outputPath,  saveMyFile);
      counter++;
    And to open files from the user defined location:
    var inFolder = new Folder("C:/Input")
    var image = new Array();
    image[0] = "image0.jpg";
    image[1] = "image1.jpg";
    image[2] = "image2.jpg";
    image[3] = "image3.jpg";
    image[4] = "image4.jpg";
    image[5] = "image5.jpg";
    image[6] = "image6.jpg";
    image[7] = "image7.jpg";
    for (var j in image) {
    if j=0 look for i=0 and add i value as text
    To simplify i'll open psd with layers, add image between existing layers and add text based on the text name which we defined in script.
    Sorry for long post, but hope I have not made this simple process confusing.
    Please do let me know if you have any questions. Hope you hear from you soon and Thanks for your time.
    Kind Regards,
    Zoogle.

  • My external hard drive is 'seen' by my iMac and I can go into the Finder and open files and folders. I am using the hard drive for Time Machine back up. However Time Machine says it can't find the drive. Same thing has happened with Final Cut Express.

    My new LaCie external hard drive is 'seen' by my iMac and I can go into the Finder and open files and folders. I am using the hard drive for Time Machine back up. However Time Machine says it can't find the drive.
    The same thing happened recently between Final Cut Express and my other LaCie external hard drive used as the Scratch disk. It fixed itself.
    I've run out of ideas. Help would be very much appreciated. Thanks.

    have you done some searches on FCPx and time machine? Is there a known issue with using a TM drive with FCPx? dunno but ...wait...I'll take 60 sec for you cause I'm just that kind of guy....   google...." fcpx time machine problem"  Frist page link 
    http://www.premiumbeat.com/blog/fcpx-bug-best-practices-for-using-external-hard- drives-and-final-cut-pro-x/
           You cannot have time machine backups on your hard drive if you intend to use it in FCPX.
    booya!

  • When I open iPhoto 9.2.1 it says that my library needs to be upgraded but when I upgrade the library I am trying to open (33 GB worth of pictures from 2006) the new application says it cannot read the files and how to find them on the system to reimport?!

    When I open iPhoto 9.2.1 it says that my library needs to be upgraded but when I upgrade the library I am trying to open (33 GB worth of pictures from 2006) the new application says it cannot read the files and how to find them on the system to reimport?! and then I'd like to erase the original files since the space requirement is huge!!!! Why is upgrading software iPhoto such a pain. I've gotta get a presentation done and all I get for my money is roadblock!!!

    hello, it sounds like the library is damaged.
    Download iPhoto Library Manager and use its rebuild function. This will create a new library based on data in the albumdata.xml file. Not everything will be brought over - no slideshows, books or calendars, for instance - but it should get all your albums and keywords back.
    Because this process creates an entirely new library and leaves your old one untouched, it is non-destructive, and if you're not happy with the results you can simply return to your old one. 

  • I have all my music cd's on a hard drive as wav files when I try to add them to I tunes the files get there but no album info I end up with a bunch of track 1's by unknown artists. Is there a way to transfer each album over so it shows up like a normal CD

    I have all my music cd's on a hard drive as wav files when I try to add them to I tunes the files get there but no album info I end up with a bunch of track 1's by unknown artists. Is there a way to transfer each album over so it shows up like a normal CD

    Try posting in a more appropriate forum, as this has noting to do with itunes U - the place where University/college/museums post education material.

  • I cannot open iTunes.  I get a message that says "The file iTunes library.itl cannot be read because it was created by a newer version of iTunes. Would you like to download iTunes now?"  I downloaded it and it and I continue to get the same message.  How

    I cannot open iTunes.  I get a message that says "The file iTunes library.itl cannot be read because it was created by a newer version of iTunes. Would you like to download iTunes now?"
    I downloaded it and it and I continue to get the same message.
    How do I fix this?    Thanks,  Dee

    Upgrade iTunes to the latest version. It should be 10.5.3.

Maybe you are looking for