I’ve managed to create a droplet which will return a parcount (number of lines) for an xml file dropped onto the script application icon. This works fine (although I had to use -1 in the code as it was originally counting an empty line). However, I’m looking for a way to show an alternative text string in the display dialog, which is dependant on the parcount value.
E.g. If the parcount is 315, then I would like the display dialog to report a different text string so that 315 displays as “This is a version 5 file”
and if parcount is 418 the display dialog would read
“This is a version 9 file”
The code I have to just display the parcount numerically is shown below. Can anyone suggest how I can achieve this (bearing in mind I want to do this via a droplet script)?
on open of theFile
set the_text to read theFile
set the_list to paragraphs of the_text
set parcount to count of the_list
display dialog ((parcount as string) - 1)
end open
Model: Mac mini
Browser: Safari 600.7.12
Operating System: Mac OS X (10.10)
Hi,
try this, it considers multiple files and subtracts -1 only if the last line is really empty and it displays also the file name.
on open theFiles
repeat with aFile in theFiles
tell application "System Events" to set fileName to name of aFile
set theLines to paragraphs of (read aFile)
set parcount to count theLines
if (length of last item of theLines) is 0 then set parcount to parcount - 1
display dialog fileName & " is a version " & parcount & " file"
end repeat
end open
Hi Stefan,
Many thanks for the quick reply. Your suggestion does not quite do what I’m looking for unfortunately. I have created another script below (which doesn’t work at all), but you hopefully see the idea behind what I’m attempting to do. I’m very much a novice at applescript.
on open theFiles
repeat with aFile in theFiles
tell application "System Events" to set fileName to name of aFile
set theLines to paragraphs of (read aFile)
set parcount to count theLines
if (length of last item of theLines) is 0 then set parcount to parcount - 1
if parcount is 316 then
display dialog fileName & " is a version 9 file" & parcount & " lines"
if parcount is 517 then
display dialog fileName & " is a version 12 file" & parcount & " lines"
end if
end if
end repeat
end open
I don’t understand the relation between 517 and 12.
Hi Stefan,
Different versions of a software application I have produce associated xml files as a user exchangeable custom resource component. The line numbers in the software version native xml files are specific to the originating version.
When these xml files are shared with other users of the software, it is the total line number in the xml version which indicate the software version the file was created in (and so whether or not the file would be compatible with a different version of the parent software application).
At the moment, I can compare the the line numbers from my/your original script with a physical list of the line numbers for each software version, and also by opening the xml file in say TextWrangler to see the line numbers, but I was looking for a more convenient way of directly reporting the associated software version via the appleScript display dialog. So, instead of displaying 517 and then having to physically consult a list to determine the original creator application was say version 12, it would just save an extra step.
If this cannot be done, it is not a major problem and your original suggestion also adds the file name, which is nice.
the first line contains the list of line numbers, please check and complement the values.
The first entry is version 1 , the second version 2 and so on.
property lineList : {0, 0, 0, 0, 315, 0, 0, 316, 418, 0, 0, 517}
on open theFiles
repeat with aFile in theFiles
tell application "System Events" to set fileName to name of aFile
set theLines to paragraphs of (read aFile)
set parcount to count theLines
if (length of last item of theLines) is 0 then set parcount to parcount - 1
set idx to count lineList
repeat while idx > 0
if item idx of lineList is parcount then exit repeat
set idx to idx - 1
end repeat
if idx = 0 then
display dialog "The version of " & fileName & " could not be identified"
else
display dialog fileName & " is a version " & idx & " file"
end if
end repeat
end open
Many, many thanks for taking the time to do this, Stefan. The indexing system works perfectly. I just added some additional zeros at the beginning of the index so that my first known version line number corresponded to version 7.
Once again, your time and expertise is much appreciated and hopefully this information will help others with a similar requirement.
regards
David