I have a script that opens a text file. It works fine but now I want it to pick up the highest numbered file. Numbers look like this vk00400 but there may be many files with the same name but with the last character having 1 or 2 so on until 9 and I want to use the one with the highest end number
can any one help me get started
tell application "Finder"
set files_ to files of (choose folder)
set compare_ to 0
repeat with file_ in files_
set last_char to (character -1 of (get name of file_)) as number
if last_char is greater than compare_ then
set compare_ to last_char
set f to file_
end if
end repeat
open f
end tell
Hi rob the script wrote for me works great but is takes a while to complete it compared to other script I have made.
I made a few mods to get it to fit with my script have I done something to cause this or is this kind of thing likely to take a while.
Files in folder are 4BNF00400.HED OR 4BNF00401.HED and so on. There can be other files in there to
my mods are:
tell application “Multiad Creator Pro”
set targetPath to “DATA:Ad Files:”
set fileName to name of document 1
end tell
tell application “Finder”
set files_ to files of folder (targetPath & characters 1 thru 7 of fileName & “:”)
set compare_ to 0
repeat with file_ in files_
set last_char to (character -5 of (get name of file_)) as number
if last_char is greater than compare_ then
set compare_ to last_char
set f to file_
end if
end repeat
end tell
tell application “Multiad Creator Pro”
activate
import text file (targetPath & characters 1 thru 7 of fileName & “:” & “f”)
REST OF SCRIPT
end tell
Have I done anything wrong as the rest of the script runs quiet fast
but this part runs alot slower.
Thank heaps Daniel
I can’t tell exactly what’s going on. When the Finder gets done with its repeat loop, the variable f will/should hold a Finder reference to a file (the file whose last number is the highest number). The Finder reference, which is recognized only by the Finder, might look something like this:
file "4BNF00400.HED" of folder "Ad Files" of folder "DATA"
This can be converted to a string or alias reference so that other applications can use it. The easiest way to do this is to change this line of code:
set f to file_
-- to
set f to (file_ as string)
This will result in a path to the file that looks like this:
"DATA:Ad Files:4BNF00400.HED"
Then, to import this file, it might look something like this:
import text file f
I don’t possess Multiad Creator Pro so I can’t debug your script to see where it might be flawed.
Instead of getting the name of each file individually, you might consider reading all the names at once. Looping through a list of names should be faster than looping through a number of files on disk.
tell application "Finder"
set names_ to (name of files of folder (targetpath & text 1 thru 7 of fileName & ":")) as list -- (better than *characters* 1 thru 7)
If the names that interest you all begin with “4BNF”, you can limit the number of names to check by extending the filter reference:
tell application "Finder"
set names_ to (name of files of folder (targetpath & text 1 thru 7 of fileName & ":") whose name begins with "4BNF") as list
File names as alike as like “4BNF00400.HED” and “4BNF00401.HED” compare lexically the same way as they do numerically, so there’s no need to extract the last numeric character of each and coerce it to number before comparing. You can just compare the names directly.
set folder_path to (targetpath & text 1 thru 7 of fileName & ":")
tell application "Finder"
set names_ to (name of files of folder folder_path whose name begins with "4BNF") as list
end tell
set higest_ to item 1 of names_
repeat with i from 2 to (count names_)
if item i of names_ is greater than highest_ then
set set highest_ to item i of names_
end if
end repeat
set f to folder_path & highest_
-- Presumed syntax for Multiad Creator Pro
tell application "Multiad Creator Pro"
activate
import text file f
REST OF SCRIPT
end tell
Nigel the script you wrote works very fast.
I have one more question
I have a date in format 30/07/04
what is the best way to return the Day and month as text
eg Monday April 30
I have the date from the text file and want it In my document but in the above format. I have a few Ideas but don’t know the best way
If you can help great.
Thanks anyway for your help
Daniel
That’s a bit confusing! Since you’re in NZ, I presume that your short date format is dd/mm/yy and that you really want a result like “Friday July 30”.
If the script’s only going to run on computers configured for short dates in day/month/year order, you could simply use:
set sDate to "30/07/04" -- in real life, this is from your text file
set {weekday:w, month:m, day:d} to date sDate
set newFormat to (w as string) & space & m & space & d
More comprehensively, if you’ve no idea how the host machine will be configured, but definitely want to convert any date from “dd/mm/yy” (assuming yy is in the “century” as when the script is run) or “dd/mm/yyyy” to “weekday month day”:
set sDate to "30/07/04"
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set {d, m, y} to sDate's text items
set AppleScript's text item delimiters to astid
-- Start with the current date to get the first two digits of the year
tell (current date)
-- Interpret the year represented by y
if y < 100 then set y to year div 100 * 100 + y
-- Change the date to the date represented by sDate
-- (Go to the previous december, set the day to (32 * m)
-- to get into the right month, then set the day to d.)
set {its month, year, day, day} to {December, y - 1, 32 * m, d}
-- Extract the weekday and the month. (We aready have the day.)
set {weekday:w, month:m} to it
end tell
set newFormat to (w as string) & space & m & space & d
If you don’t intend to run the script before 01/01/2000 or after 31/12/2099, you can change ‘tell (current date)’ to ‘tell date "“Saturday, 1 January 2000 00:00:00”’, which will make it run faster. Unbelievably, on my machine, this faster time is even faster than with the first of the above scripts!