Pointing to a SDHC card SubFolder

I am trying to get how many files has the extension “MPG” on a SDHC card.

The SDHC card name is NX5U
The folder I am trying to get at is /MP_ROOT/101PNV01"
The files contained on that subdirectory has the extension MPG

The following script will give the correct number of mpg files that are not located on a subfolder on the NX5U SDHC card.

What would be the AppleScript syntax for pointing to a specific NX5U subfolder?

Thanks!
Daniel

Hmmm, well you could list all the folders inside the card, and then loop through them one by one and look for other folders or files.

Something like this (will need adapting):

set the usr_fonts_folder to the path to fonts folder from user domain

tell application "Finder" to set userFontsFiles to (name of every file in usr_fonts_folder) as list
tell application "Finder" to set userFontsFolders to (name of every folder in usr_fonts_folder) as list

Would this help?

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Thanks Leon,

I do not need to go through all folders but to /MP_ROOT/101PNV01.

The camera will always create MPG files in the /MP_ROOT/101PNV01 subdirectory of the sdhc card call NX5U. I have been able to go to a specific folder located on the MacBook hard drive by typing the following:

However, when trying for a specific folder on the SDHC card, it is not the same story. I’ve tried using a command such as

and I get the error message "Error in Finder : the disk is not accessible “NX5U:MPROOT:101PNV01”. In french it says’s Erreur dans Finder : Il est impossible d’obtenir disk “NX5U:MPROOT:101PNV01”.

Once, I will be able to goto this subdirectory, I will then need to look if a specific file exist. However, my current difficulty is really to point to this subdiretory. I do not seem to be able to get the syntax correct.

Regards!
Daniel

The computer says that the disk is not accessible. Maybe a wrong name? If you execute this code, is the name NX5U in in the returned list?

list disks

Hello

I have made a script for pasting paths into AppleScript (as many others) it is located here.

You should install the script under Applications/Finder, so that it is accessible under finder.
Then choose the folder in question. Invoke the script, choose HFS path, then paste it into your script.
You should then be able to use it within or without a finder block to access your folder.

Alternatively you could drag the folder into a terminal window, you should then get the Posix path of the folder.

Best Regards

McUsr

I just realized that you use this command to list the contents of a folder:

(files in disk "NX5U:MPROOT:101PNV01" whose name extension is "mpg")

If i’m not mistaked, you are asking to list the content of a folder by addressing it as a disk. NX5U is the disk’s name, the rest is a Finder-kind path.

Did you try path or folder instead of disk? Might make the difference…

And also, I am pretty sure this command would work too (tested with a different path and extension and it worked):

tell application "Finder" to set listOfFiles to (name of every file in "NX5U:MPROOT:101PNV01:" whose name extension is "mpg") as list

Don’t forget the : at the end of the path, after 101PNV01. Your code did not include it…

Browser: Safari 531.9
Operating System: Mac OS X (10.6)

Hi,
It might work, but it’s actually wrong syntax. You’re going to gather the files of a literal string.
AppleScript is so kind and treats the string as a folder because of the trailing colon, but you should add the specifier keyword in front of the string

tell application "Finder" to set listOfFiles to (name of every file in folder "NX5U:MPROOT:101PNV01:" whose name extension is "mpg")

the as list coercion is not needed

@ Stefank: Thanks for the info. It may return a string like you said, but in my test I ended up with a list of only the file names, as I believe is desired here. Maybe, like you said, AppleScript is kind enough to fix our mistakes…

@ Daniel Paquin: I would suggest that you put the command in a try on error loop, and if the folder is not present for any reason it would display a dialog asking to choose a folder…

Browser: Safari 531.9
Operating System: Mac OS X (10.6)

I really appreciate your help.

I’ve written a pseudocode describing what I am trying to do.

Goto folder MPROOT:101PNV01 of SDHC card name NX5U
set cnt to 1
set cntnewfile to 0
set FileInitiator to “MU200” & cnt & “00.MPG” --MU200100.MPG
Do loop
if FileInitiator exist then
do
cntnewfile = cnt + 1
If cnt is greater than 9 --this is is necessary because I need to keep the leading 0
then do
set filetorename to “MU20” & cntnewfile & “00.MPG” – MU201000.MPG
set findmpg to “MU20” & cntnewfile & ".MPG" – MU2010.MPG
end
else do
set filetorename to “MU200” & cntnewfile & "00.MPG – MU200100.MPG
set findmpg to “MU200” & cntnewfile & ".MPG – MU2001.MPG
rename FileInitiator to filetorename – rename MU200100.MPG to MU200200.MPG
end
cnt + 1
End Loop
then set cntmpg equal to List how many MPG files with name equal to findmpg

Open /Macinotshd/user/Filetocompile.txt
got to end of file
write "There are " & cntmpg & " files with name equal to " & findmpg
Close Filetocompile

There should always be a file on sdhc card with the following naming convention
MU200000.MPG

I need to look if the file MU20xx00.MPG exist
if so I need to rename it to MU20 xx+1 00.MPG keeping the leading 0
if not increment xx by 1 and do the test again until the file is found

In doing so the next time I press record on my camera the video file will be created such
as MU200101.MPG, MU200102.MPG, MU200103.MPG…

In renaming MU200100.MPG to MU200200.MPG the next time i will press record I want the video file to be name
as MU200201.MPG, MU200202.MPG, MU200203.MPG… and so on

In renaming file with MU20 xx+1 00.MPG I am controlling how the file will be name by the camera.

THANKS IN ADVANCE!
Daniel

P.S.: I do have a applescript which can open, write and close a file on my hard disk. I am really having a problem to point to a folder onto my SDHC card name NX5U. The SDHC card does not come with the name NX5U, I did it myself. The reason being is that I have the application called “Do Something When” which will automatically start this program when NX5U is detected.

This is a start of what I am trying to do

property logname : "M2U01424.MPG"

tell application "Finder"
	with timeout of 0 seconds
		set filePath to (folder "MP_ROOT:101PNV01" of disk "NX5U" as string) & logname
		if file filePath exists then
			display dialog "File exist"
		else
			display dialog "file does not exist"
		end if

The file M2U01424.MPG located on the NX5U SDHC card has been found. I now have to work on the loop and see if I can do something with the leading 0 because the file name is always 8 characters long and I only need to increase the number by 1 in position 5-6 (keeping the leading 0) and have “0” in position 7 and 8.

Hello.

I wonder if I have understood this correctly.

If the questions seems silly please bear over with me.

You are either having a lot of video files on your card, or you are going to move those used files to
another location. So that when your script runs, there will just be one MU20xx00.MPG file there.
That file was from the previous batch of videos, and now you want to increase that number on that
on file by renaming it in order to start a new batch which will be in chronological ascending order with the previous series of videos that you have recorded.

The file that is renamed, is not having any content, it is just a template for naming the videos to be recorded.

Is this basically right?

Best Regards

McUsr

You are absolutely right McUsr.

I could not have explained better than you did.

There will be only one MU20xx00.MPG where xx correspond to a batch. There will never be more than 99 video in a batch and there will not be more than 99 batch. Then if for that week-end there are 59 special events there will be 59 batch.

The first batch will start at MU200100.MPG and the last batch at MU205900.MPG.

To force the camera to restart at MU200100.MPG, I will have to make the last event to be MU209999.MPG. In doing so the camera create a new folder 101PNV02 where the first video file is name MU200001.MPG. I only have to rename 101PNV02 to 101PN01 and rename MU209999.mpg to MU200100.MPG and I am OK for another week-end.

In finding how many mpg file with MU20xx*.mpg I will be getting how many videos have been recorded for a certain batch.

Thrue, I will be having a lot of video files on the SDHC card and I will be moving them to another location. I will be using up to 6 SDHC card on a week-end, all of them name NX5U. It is important for me to control the name the camera will be recording video under.

Having the name as NX5U will tell to the “Do Something When” application which AppleScript to start.

Best regards!
Daniel

Hello.

Se if you can use this:


tell application "Finder"
	set endStr to "00.MPG"
	set listOfFiles to (sort (every file in folder "NX5U:MPROOT:101PNV01:"  whose name extension is "MPG") by name) 
	set countOfListOfFiles to count of listOfFiles
	if listOfFiles = {} then
		display dialog "There is no MPEG files on the card"
		error number -128
	else
		set countInBatch to 0
		set foundBatchStarter to false
		repeat with i from countOfListOfFiles to 1 by -1
			if (contents of item i of listOfFiles as text) ends with endStr then
				set foundBatchStarter to true
				exit repeat
			else
				set countInBatch to countInBatch + 1
			end if
		end repeat
		set limitReached to false
		if foundBatchStarter is false then
			display dialog "Didn't find the initator, I don't know what to do, I'm aborting this!"
			error number -128
		else -- there was something more to be done
			set fileInitiator to item (countOfListOfFiles - countInBatch) of listOfFiles
			set fileInitiatorName to name of fileInitiator
			set curBatchNumber to characters 3 thru -7 of fileInitiatorName as text as number
			if curBatchNumber is equal to 205900 then 
				display dialog "The limit is reached: (205900) please manually adjust before commencing" buttons {"Ok"}
				set limitReached to true
			end if 
			if not limitReached then 
				set newBatchNumber to (curBatchNumber + 1) as text
				set newInitiatorName to "MU" & newBatchNumber & endStr
				set name of fileInitiator to newInitiatorName
			end if ” We sum up the files of the last batch anyway.
			set  logFName to "/Macinotshd/user/Filetocompile.txt" -- you did specify this file with a posix path
			try
				set theLogFile to POSIX file logFName as alias -- If you are not then remove "Posix file" from this line.
			on error
				display dialog "Couldn't find logfile Aborts writing logfile"
				error number -128
			end try
			try
				set lf to open for access theLogFile with write permission
				set eofMarker to get eof of lf
			on error
				display dialog "I got an error accessing the logfile .. Exits"
				try
					close access lf
				end try
				error number -128
			end try
			set eofMarker to eofMarker + 1
			set theString to "There are " & countInBatch & " files with name equal to " & fileInitiatorName & return & (character id 10)
			try
				write theString to lf starting at eofMarker
				close access lf
			on error
				display dialog "I got an error wrting to the  logfile .. Exits"
				try
					close access lf
				end try
				error number −128
			end try
		end if
	end if
end tell

You wrote:

I haven’t done anything with this for a reason. This is things that I feel you should do manually in order to not create disorder
-I believe that this automatic “rollover” can lead to a real mess especially if you haven’t moved the video files from the previous batch with that name. I don’t want to have any part in that. So in order to start afresh you will have to manually rename the file to MU200100.MPG

Best Regards

McUsr

I want to give a special thank to McUsr :slight_smile:

The appleScript McUsr has provided really helped me.

The most important function to increase (xx) in the video file name MU20xx00.MPG works perfectly.

The only part I have to modify is the record being written in the file “Filetocompile.txt”.

There are 32 files with a MPG extension on the SDHC card.
Batch 13 with two video files, batch 14 with 29, and batch 15 with one.

I am having the M2U01200.mpg as a stater (first batch) on the SDHC card and the program write :
for batch 12 - 32 files when it should be 0
for batch 13 - 32 files when it should be 2
for batch 14 - 29 files - this is OK
for batch 15 - 1 files - this is OK
for batch 16 - 0 files wich is OK
for batch 17 - 0 files wich is OK

Once, I’ve figured this one out I will post the information.

Regards!
Daniel

property logname : "Filetocompile.txt"

tell application "Finder"
	set endStr to "00.MPG"
	set listOfFiles to (sort (every file in folder "NX5U:MP_ROOT:101PNV01:" whose name extension is "MPG") by name)
	set countOfListOfFiles to count of listOfFiles
	
	if listOfFiles = {} then
		display dialog "There is no MPEG files on the card"
		error number -128
	else
		set countInBatch to 0
		set foundBatchStarter to false
		repeat with i from countOfListOfFiles to 1 by -1
			if (contents of item i of listOfFiles as text) ends with endStr then
				set foundBatchStarter to true
				exit repeat
			else
				set countInBatch to countInBatch + 1
			end if
		end repeat
		
		set limitReached to false
		if foundBatchStarter is false then
			display dialog "Didn't find the initator, I don't know what to do, I'm aborting this!"
			error number -128
		else -- there was something more to be done
			set fileInitiator to item (countOfListOfFiles - countInBatch) of listOfFiles
			set fileInitiatorName to name of fileInitiator
			set curBatchNumber to characters 4 thru -7 of fileInitiatorName as text as number
			if curBatchNumber is equal to 99 then
				display dialog "The limit is reached: (99) please manually adjust before commencing" buttons {"Ok"}
				set limitReached to true
			end if
			if not limitReached then
				set newBatchNumber to (curBatchNumber + 1) as text
				if curBatchNumber is less than "9" then
					set newInitiatorName to "M2U00" & newBatchNumber & endStr
				else
					set newInitiatorName to "M2U0" & newBatchNumber & endStr
				end if
				set name of fileInitiator to newInitiatorName
			end if -- We sum up the files of the last batch anyway.
			
			set logFName to (folder "Users:UserName:VideoCamera:" of startup disk as string) & logname
			try
				set theLogFile to logFName as alias -- If you are not then remoe "Posix file" from this line.
			on error
				display dialog "Couldn't find logfile Aborts writing logfile"
				error number -128
			end try
			try
				set lf to open for access theLogFile with write permission
				set eofMarker to get eof of lf
			on error
				display dialog "I got an error accessing the logfile .. Exits"
				try
					close access lf
				end try
				error number -128
			end try
			set eofMarker to eofMarker + 1
			set theString to "There are " & countInBatch & " files with name equal to " & fileInitiatorName & return & (character id 10)
			try
				write theString to lf starting at eofMarker
				close access lf
			on error
				display dialog "I got an error wrting to the logfile .. Exits"
				try
					close access lf
				end try
				error number -128
			end try
		end if
	end if
end tell

Hello.

The code as was posted was only intended to take care of one batch at a time.
I’ll try to make another version which cater for processing of more than one batch at a time if you have renamed the
MU00XX00.MPG file yourself (and thereby generated other batches) before you run the script again.
The script as it was made, was only intended to take care of the latest batch. I’m sorry that I didn’t anticipate that.

Best Regards

McUsr

McUsr, you sure do not have to be sorry about this.

I am way ahead because of your great help. Not only you’ve did you provide a script program but it is a good program.

I am learning a lot in just getting to understand the code. You’ve made it to trap errors that could occur nobody could have asked for better than this.

THANKS AGAIN!
Daniel

Hello.

Here is a new version that takes care of some of the problems with the last one.
There is really no way here to catch batches with 0 items, I hope that won’t be a problem for you.
Heck! Ill make it for you if state that you really can’t live without it. (But not before :wink: )
I think I have tested it thoroughly, but please don’t hesitate to tell me if there are any problems with it.


property logname : "Filetocompile.txt"
property videoFilePath : "NX5U:MP_ROOT:101PNV01:"
tell application "Finder"
	set startStr to "MU20"
	set endStr to "00.MPG"
	
	set listOfFiles to (sort (every file in folder videoFilePath whose name extension is "MPG") by name)
	set countOfListOfFiles to count of listOfFiles
	if listOfFiles = {} then
		display dialog "There is no MPEG files on the card"
		error number -128
	else
		-- has video files, must look for files in batch with greater number than ours, or start back at 0100 if last
		-- number was 9900 
		-- will read last logfile entry to see which entry was the last logged. will commence thereon 
		-- until last batch entered.
		set logFName to "Users:UserName:VideoCamera:"
		try
			set loggedList to read file logFName as list using delimiter {return & (character id 10)}
		on error
			display dialog "Couldn't read logfile Aborts the Script"
			error number -128
		end try
		set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {return & (character id 10)}}
		set lastBatch to text item -2 of loggedList
		set loggedList to null -- disposes the list after getting what we wanted
		set AppleScript's text item delimiters to {" files with name equal to "}
		set lastBatch to text item 2 of lastBatch -- the part of the line with the initiator filename in
		set AppleScript's text item delimiters to astid
		set lastProceccedNumber to characters 5 thru 6 of lastBatch as text as number
		-- We have found the last batch number we processed, we know we have some files, we should start
		-- looking for the next batch number. This is really a different story compared with previous version.
		-- first we must skip any previously processed file.
		set stemname to startStr & my twoDigitStr(lastProceccedNumber)
		set filesToSkip to 1 -- we are using this one thru rest of script to keep track of starting point in listOfFiles
		-- it is allways pointing to an element one higher, but that turns out ok. If = 1 -> no skip at all.
		repeat with i from 1 to countOfListOfFiles
			set thisFileName to name of (get contents of item i of listOfFiles) as text
			set curnumber to characters 5 thru 6 of thisFileName as text as number
			if curnumber ≤ lastProceccedNumber then
				set filesToSkip to filesToSkip + 1
			else
				exit repeat
			end if
		end repeat
		set nrOfFilesToProcess to countOfListOfFiles - filesToSkip + 1 -- adjusts for the false skipping. 
		-- curnumber now correctly denotes the first number to process.
		local limitReached, countInBatch
		
		if nrOfFilesToProcess is 0 then
			set curnumber to curnumber + 1 -- in order to get a new current number when nothing is done.
		end if
		repeat while nrOfFilesToProcess > 0
			set stemname to startStr & my twoDigitStr(curnumber)
			set foundBatchStarter to false
			set countInBatch to 0
			repeat with i from filesToSkip to countOfListOfFiles
				if (contents of item i of listOfFiles as text) contains stemname then
					set countInBatch to countInBatch + 1
				else
					exit repeat
				end if
			end repeat
			
			if countInBatch > 0 then
				set fileInitiatorName to stemname & endStr
				if (countInBatch + filesToSkip - 1) = countOfListOfFiles then
					
					try
						(videoFilePath & fileInitiatorName) as alias
						-- we got so we decrease number of count in batch
						set countInBatch to countInBatch - 1 -- Doesn't count the initiator file
						set filesToSkip to filesToSkip + 1 -- adjust for the one above, in order to finalize processing of this.
					end try
				end if
				-- would never get this far if we didn't have a logfile in the first place Doesn't test if it exists.	
				set theLogFile to logFName as alias
				try
					set lf to open for access theLogFile with write permission
					set eofMarker to get eof of lf
				on error
					display dialog "I got an error accessing the logfile .. Exits"
					try
						close access lf
					end try
					error number -128
				end try
				set eofMarker to eofMarker + 1
				set theString to "There are " & countInBatch & " files with name equal to " & fileInitiatorName & return & (character id 10)
				try
					write theString to lf starting at eofMarker
					close access lf
				on error
					display dialog "I got an error wrting to the logfile .. Exits"
					try
						close access lf
					end try
					error number -128
				end try
				
			end if
			set curnumber to curnumber + 1
			set filesToSkip to filesToSkip + countInBatch
			set nrOfFilesToProcess to countOfListOfFiles - filesToSkip + 1 -- adjusts for the false skipping. 	
			
		end repeat
		set limitReached to false -- if we have reached last number in batch
		if curnumber is equal to 100 then
			display dialog "The limit is reached: (99) please manually adjust before commencing" buttons {"Ok"}
			set limitReached to true
		end if
		if not limitReached then
			set newInitiatorName to startStr & my twoDigitStr(curnumber) & endStr
			set name of file (videoFilePath & fileInitiatorName) to newInitiatorName
		end if
		
	end if
end tell

on twoDigitStr(theNumber)
	if theNumber ≤ 9 then
		return "0" & theNumber
	else
		return theNumber as text
	end if
end twoDigitStr


Best Regards

McUsr

Dear McUser,

I totally agree with this comment

I may have mislead you in my previous post. I AM VERY SORRY ABOUT THIS!

I am not reading the file “Filetocompile.txt”.

In executing the applescript program 8 times (one execution after the other) with M2U00900.MPG to start the program writes in Filetocompile.txt the following:

On the SDHC card there are only three batches (13, 14, and 15) for which I have video files. The AppleScript programt should have written

Best Regards!
Daniel

Hello.

Does it really do that with the last version I posted?

Do you rename the initiator file or do you create a new one for each batch, when you are using the camera to
start a new batch?

Best Regards

McUsr

For the first batch, I will be copying onto the SDHC card (NX5U disk) a file called M2U00100.MPG. This file will be stored on my MacBook.

All of the other batches will be triggered by renaming M2U00100.MPG to M2U00200.MPG and so on up to a maximum of 99 batches.

The first applescript code was performing this operation perfectly.

I am not sure, when trying the code I am getting an error message. At this moment it is when trying to read the logfile I am getting the message “Couldn’t read logfile Aborts the script”. The file is there but not sure to know which format it should be.

Thanks!
Daniel