Applescript if files inside of folder exists then else

Hi im struggling to get this code to work.

set a to "Macintosh HD:Users:User:Movies:Test"

if files inside of a folder of folder a exists then

mount volume "nfs://10.0.1.50/disk1"

else

eject disk1


it just need to look inside the first folder.

Posted on Feb 18, 2020 7:15 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 18, 2020 10:12 AM

There are a couple of flaws in your script, I'll try to highlight some of them:


First:


> set a to "Macintosh HD:Users:User:Movies:Test"


Don't use 'a' as a variable name. It really confuses things on the line:


> if files inside of a folder of folder a exists then


is 'a folder' referring to the variable a as a folder, or is it referring to 'some/one/any' folder as we might use in conversation "I'd like a cookie, please'... you don't generally care which cookie, any of them will do.

Similarly, on the same line you're referring to 'folder a' which I assume is referring to the variable, but since my mind is already messed up based on the previous sentence, I'm not sure. Solution: use more descriptive variable names.


Given the above, I think the line:


if files inside of a folder of folder a exists then


means 'if there are any files inside ((any non-specific folder) of the designated folder)...'


If that's the case, I'd go for something like:


set folderPath to (path to movies folder as text) & "Test:"


tell application "Finder"

set subFolderCount to count folders of folder folderPath

if subFolderCount > 0 then

set subFileCount to count files of folder 1 of folder folderPath

if subFileCount > 0 then

mount volume "nfs://10.0.1.50/disk1"

else

eject disk "disk1"

end if

end if

end tell


It's a lot wordier than it could but, but the logic is simple to follow and easy to amend.


Similar questions

7 replies
Question marked as Top-ranking reply

Feb 18, 2020 10:12 AM in response to dkaiser92

There are a couple of flaws in your script, I'll try to highlight some of them:


First:


> set a to "Macintosh HD:Users:User:Movies:Test"


Don't use 'a' as a variable name. It really confuses things on the line:


> if files inside of a folder of folder a exists then


is 'a folder' referring to the variable a as a folder, or is it referring to 'some/one/any' folder as we might use in conversation "I'd like a cookie, please'... you don't generally care which cookie, any of them will do.

Similarly, on the same line you're referring to 'folder a' which I assume is referring to the variable, but since my mind is already messed up based on the previous sentence, I'm not sure. Solution: use more descriptive variable names.


Given the above, I think the line:


if files inside of a folder of folder a exists then


means 'if there are any files inside ((any non-specific folder) of the designated folder)...'


If that's the case, I'd go for something like:


set folderPath to (path to movies folder as text) & "Test:"


tell application "Finder"

set subFolderCount to count folders of folder folderPath

if subFolderCount > 0 then

set subFileCount to count files of folder 1 of folder folderPath

if subFileCount > 0 then

mount volume "nfs://10.0.1.50/disk1"

else

eject disk "disk1"

end if

end if

end tell


It's a lot wordier than it could but, but the logic is simple to follow and easy to amend.


Feb 18, 2020 12:36 PM in response to dkaiser92

> both script also takes folders into as count as files


I'm not quite sure what you mean - my script first checks to make sure there are subfolders in the specified folder, then it checks to see if there are any files in the first folder. That's what it sounded like you were looking for.


In general, the script can be tweaked to do anything - you just need to be specific about what you want.


For example, if you want to check if there are any items in the subfolder, rather than just files, change the line:


set subFileCount to count files of folder 1 of folder folderPath

to:

set subFileCount to count items of folder 1 of folder folderPath


Now any item in the subfolder will trigger.


> how do i do if it also need to check if it need to be a ".scpt" file


You mean check if there is a .scpt file in the folder? Again, be specific. Change the line:


set subFileCount to count files of folder 1 of folder folderPath

to:

set subFileCount to count (every file of (folder 1 of folder folderPath) whose name ends with ".scpt")


Feb 18, 2020 12:59 PM in response to Camelot

im sorry , i did not think it would be a problem before i tested it ,, can't get that to code to work . hmm


set folderPath to (path to movies folder as text) & "Test:"

tell application "Finder"

set subFolderCount to count folders of folder folderPath

if subFolderCount > 0 then

set subFileCount to count (every file of (folder 1 of folder folderPath) whose name ends with ".scpt")

if subFileCount > 0 then

display dialog "true"

else

display dialog "false"

end if

end if

end tell

Feb 18, 2020 9:00 AM in response to dkaiser92

Give the following code a try.


-- representation of "Macintosh HD:Users:User:Movies:Test"
set a to ((path to home folder as text) & "Movies:" & "Test:") as alias

tell application "Finder"
	-- check if folder a has any contents
	if not items of a is {} then
		mount volume "nfs://10.0.1.50/disk1"
	else if (list disks) contains "disk1" then
		try
			eject "disk1"
		end try
	end if
end tell






This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript if files inside of folder exists then else

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.