johnfromcoronado

Q: Change Filename of Selection to "Import.xls" in Applescript

Hi, I'm way back in Tiger, but I am trying to change the name of a file in a folder (which may be whatever name it is) to a known name.  Ultimately, I'm just trying to import a known file to filemaker and need the name to be a known name.  This is what i Have thus far, but I'm just not having success.  This goes to the folder window 2 and selects the first file...no problem, but from there I can't seem to get it to change the name to "import.xls".  Only excel files will be present in this folder, so I'm not worried about that. 

 

The other thing I would like to accomplish is if there is no file in that folder than skip over everything and do nothing.

 

Thanks for your help!!!

 

 

tell application "Finder"

activate

set target of Finder window 2 to folder "Orders_to_Enter" of folder "Documents" of folder "MINIJOHN"of folder "Users" of startup disk

select first file of Finder window 2

try

set the name of the selection to "IMPORT.xls"

end try

end tell


 

tell

Mac mini, Mac OS X (10.6.8), Applescript

Posted on Oct 4, 2016 11:51 PM

Close

Q: Change Filename of Selection to "Import.xls" in Applescript

  • All replies
  • Helpful answers

  • by Hiroto,Solvedanswer

    Hiroto Hiroto Oct 5, 2016 1:41 AM in response to johnfromcoronado
    Level 5 (7,348 points)
    Oct 5, 2016 1:41 AM in response to johnfromcoronado

    Hello

     

    No need to manipulate selection. You may simply write something like this:

     

    set d to (path to documents folder as string) & "Orders_to_Enter:" as alias
    tell application "Finder"
        --open item d -- optional
        tell (item d's file 1 whose name extension = "xls")
            try
                set name to "IMPORT.xls"
            end try
        end tell
    end tell
    

     

     

    Tested under OS X 10.6.8.

     

    Good luck,

    H

  • by johnfromcoronado,

    johnfromcoronado johnfromcoronado Oct 5, 2016 8:30 AM in response to Hiroto
    Level 1 (8 points)
    Oct 5, 2016 8:30 AM in response to Hiroto

    This isn't just an elegant solution, it's absolute poetry!  Of course I tried it and it worked, but I used the same methodology to delete files in specific folders, move files from one folder to another, and it all worked perfectly.  The only thing I couldn't figure out yet is when it selects the file 1 of 'd', I tried to make sure that that folder was sorted in order of modification date.  It seems to always grab the file sorted by name.  Thank you so much!

     

    tell application "Finder"

       set theItems to name of every file of folder d

       sort theItems by modification date

    end tell



  • by Hiroto,

    Hiroto Hiroto Oct 5, 2016 7:49 PM in response to johnfromcoronado
    Level 5 (7,348 points)
    Oct 5, 2016 7:49 PM in response to johnfromcoronado

    Hello

     

    To get the last modified item, you can use "sort" command in Finder as follows.

     

    Two things to remember.

     

    1) Finder's sort command can only be applied to list of Finder items and it returns sorted list without modifying the source list.

     

    2) Finder's sort by (modification / creation) date behaviour has been changed under 10.9. Until 10.9, it returns the newest first list. Under 10.9 or later, it returns the oldest first list.

     

     

    set d to (path to documents folder as string) & "Orders_to_Enter:" as alias
    tell application "Finder"
        -- sort by modification date
        tell (item d's files whose name extension = "xls")
            set ff to sort it by modification date
        end tell
        
        -- the last modified
        (*
        tell ff's item 1 -- OS X 10.8.x or earlier
            try
                set name to "IMPORT.xls"
            end try
        end tell
        tell ff's item -1 -- OS X 10.9 or later
            try
                set name to "IMPORT.xls"
            end try
        end tell
        *)
        tell ff's item (my newest_index_in_sorted_by_Finder()) -- auto-switch
            try
                set name to "IMPORT.xls"
            end try
        end tell
    end tell
    
    on newest_index_in_sorted_by_Finder()
        (*
            return integer : index of the newest item in the sorted by (modification/creation) date by Finder
            
            * Finder's sort command's behaviour is suddenly changed under 10.9.
              It's been the newest first in its entire history until 10.9
              but now it's been changed to the oldest first (without any notice)
        *)
        considering numeric strings
            if (system info)'s system version < "10.9" then
                return 1
            else
                return -1
            end if
        end considering
    end newest_index_in_sorted_by_Finder
    

     

     

     

    Good luck,

    H