Problem listing assets inside a directory list - list

I need to do list of assets that are inside a list of directories. So, the step-by-step is this:
The user selects a Parent Directory which contains all the other
directories with assets and then makes a list with these directories;
Then, I have to access the first directory of the list and make
another list with the assets inside the directory.
The problem is that when I run the code for the first time without the loop, it made the list of directories perfectly. But when I try to run with the loop the Scripting Listener says:
"Error occurred in anonymous codeblock; filename:
E:\MUVA\MaxScript\teste.ms; position: 371; line: 9
-- No "map" function for "\*""
And then I have to restart the 3ds Max and delete the loop for the first step works again. Maybe there's something wrong with my logical that I'm not seeing.
dirPath = getSavePath caption: "SELECT THE PARENT DIRECTORY"
dirList = getDirectories dirPath + "\*"
print ("Directories: " + dirList)
for dir in dirList do(
assetsPath = dir + "\*.max"
assetsList = getFiles assetsPath
print ("List of assets: " + assetsList)
)

And yes, my logical was wrong. I update the code and get ot the result I want.
directoryParent = getSavePath caption: "SELECT THE PARENT DIRECTORY"
dirPath = directoryParent + "\*"
dirList = getDirectories dirPath
print dirList
for dir in dirList do(
assetsPath = dir + "\*.max"
assetsList = getFiles assetsPath
print assetsList
)

Related

Applscript- Use List to Select Specifc Photoshop Action

Ive been trying to write a Applescript using the bits of knowledge I have
current stumbling blocks are
-getting the returned list selection to run the photoshop action
-how to repeat the action on multiple images.
Aim
I want to use a list to extract different cobinations of files (with set naming conventions) from a defined folder,
I would then like that same list selection to choose between mutliple photoshop actions and run the extracted file combination through that action.
Stage 1
-on running open a list
-List to conatain a set of names relating to photoshop actions
-select from list
Stage 2
-choose folder with source images (always 14 images always with the same last 9 characters _0000.tif to _0013.tif)
-Choose a save folder
Stage 3
-dependant on original list selection, automatically gather files from source image folder and run them through a coresponsing photoshop action
e.g If "Action 1" selceted from List select image "_0001.tiff & _0010.tif" from source folder and do photoshop action "Action1"
Stage4
save in chosen "save folder"
The Script So Far
--Stage 1--
set PhotoshopActionList to {"Action1", "Action2", "Action3", "Action4", "Action5"}
set ActionrequiredAnswer to choose from list PhotoshopActionList with title "Actions Picker" with prompt "Choose Action?"
if ActionrequiredAnswer is false then
error number -128 (* user cancelled *)
else
set ActionrequiredAnswer to ActionrequiredAnswer's item 1 (* extract choice from list*)
end if
end run
--Stage 2--
property SourceFolder : missing value
property destinationFolder : missing value
if SourceFolder = missing value then
set SourceFolder to (choose folder with prompt "Choose Base Images:")
set destinationFolder to (choose folder with prompt "Choose Save Location:")
else
tell application "Finder"
set theFolders to every item of entire contents of SourceFolder as list
repeat with thisFolder in theFolders
make new alias file at destinationFolder to thisFolder
end repeat
end tell
end if
--Stage 3--
tell application "Finder"
set filesList to {files of entire contents of SourceFolder contains "_001", "_002", "003"} as alias list
end tell
tell application "Adobe Photoshop"
repeat with aFile in filesList
open aFile
do action "Action1" from "Actionsfolder"
end tell
--Stage 4--
save currentDocument in folder destinationFolder as JPEG
I did not found a way to select entire contents of folder AND filter extension 'tif', 'tiff',.. AND filter files whose name contains your patterns.
As work around, I did 2 steps:
1) select in entire contents only files with target extensions.
2)I loop through these files to check is file name contains the target pattern. This is done by routine FnameOK.
You need to complete the script bellow with your Photoshop action and the 'save as':
set PhotoshopActionList to {"Action1", "Action2", "Action3", "Action4", "Action5"}
set ListOK to {"_001", "_002", "003"}
set ActionRequiredAnswer to choose from list PhotoshopActionList with title "Actions Picker" with prompt "Choose Action?"
if ActionRequiredAnswer is false then
error number -128 (* user cancelled *)
else
set ActionRequiredAnswer to ActionRequiredAnswer's item 1 (* extract choice from list*)
end if
set SourceFolder to (choose folder with prompt "Choose Base Images:")
set DestinationFolder to (choose folder with prompt "Choose Save Location:")
tell application "Finder" to set filesList to files of entire contents of SourceFolder whose name extension is in {"tiff", "tif"}
repeat with aFile in filesList
tell application "Finder" to set NameF to name of aFile
if FNameOK(NameF, ListOK) then -- the file name contains a valid pattern, then process the file
tell application "Adobe Photoshop"
open (aFile as alias)
-- perform action selected
-- save as to Destination Folder
end tell
end if
end repeat
on FNameOK(Local_Name, LocalList) -- check if the file name contains an element of the list
set LocalCheck to false
repeat with OneItem in LocalList
if Local_Name contains OneItem then
return true
end if
end repeat
return false
end FNameOK

Delete all the files after copying in new folder

'Start Button' does the following:
Check the given folder path for .blf files
Copy all the existing .blf files to the destination folder
Show the directory of the folder in text file i,e. where the files are being created
Show the directory where the existing files are being copied i,e. Onto the server
Delete the file from parent folder after copying
How can i do the Point 5..where am i at fault?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Const DestinationDirectory As String = "C:\Users\nha4abt\Desktop\Move_Here"
'Show the parent folder path
TextBox1.Text = "C:\Users\nha4abt\Desktop\Ahmad_examaning_folder"
' Show the destination folder path
TextBox2.Text = DestinationDirectory
For Each FileName As String In directory.GetFiles("C:\Users\nha4abt\Desktop\Ahmad_examaning_folder", "*.blf")
File.Copy(FileName, Path.Combine(DestinationDirectory, Path.GetFileName(FileName)))
ListBox1.Items.Clear()
ListBox1.Items.Add(FileName)
Next FileName
For Each FileName In As String In directory.GetFiles("C:\Users\nha4abt\Desktop\Ahmad_examaning_folder", "*.blf")
File.Delete(Path.GetFileName(FileName))
Next FileName
End Sub
I did that and got the result. It was more like a silly mistake.
Const DestinationDirectory As String = "C:\Users\nha4abt\Desktop\Move_Here"
'Show the parent folder path
Const ParentDirectory As String = "C:\Users\nha4abt\Desktop\Ahmad_examaning_folder"
TextBox1.Text = ParentDirectory
' Show the destination folder path
TextBox2.Text = DestinationDirectory
ListBox1.Items.Clear()
For Each FileName As String In Directory.GetFiles(ParentDirectory, "*.blf")
File.Copy(FileName, Path.Combine(DestinationDirectory, Path.GetFileName(FileName)))
File.Delete(FileName)
ListBox1.Items.Add(FileName)
Next FileName
End Sub

Appending tuple to an empty list

if __name__ == '__main__':
website_path = GetValidDirectory("Enter the path to the website : ")
websitelink_list = [] #List of tuples containing the source link information
print(websitelink_list)
htmlfiles = [] #List of the html files we analyze on the site
for dirname, subdir_list, file_list in os.walk(website_path):
for file in file_list:
#Check the file extension. Only analyze files that are .html
filename, fileext = os.path.splitext(file)
if fileext == ".html":
relative_file = MakeFullPath(dirname, file)
full_file = os.path.realpath(relative_file)
htmlfiles.append( (full_file, relative_file) )
filelinks = FileLinkTuples(dirname, file)
print(filelinks)
websitelink_list.extend(filelinks)
#Files that do not have a link to them.
functions:
def GetValidDirectory(prompt : str) -> str:
""" Returns a valid directory entered from the user.
Arguments :
prompt : str - The text displayed to the user asking them to enter a directory.
Returns : String of a valid directory that the user entered.
Errors : Displays a warning if the user does not enter a valid directory.
"""
while True:
try:
str_prompt = input(prompt)
prompt_dir = os.path.realpath(str_prompt)
ch_dir = os.chdir(str_prompt)
except IOError:
print("Please enter a valid directory")
continue
return prompt_dir
def MakeFullPath(path : str, filename : str) -> str:
""" combines path and filename for a full filename. If path does not end in \\ then it appends it
Arguments :
path : str, the path to the file
filename : str, the name of the file.
Returns :
path concatenated with filename.
If path does not end with a backslash, then append one if path has data in it.
"""
no_data = []
if os.listdir(path) != no_data:
relative_file = os.path.join(path,filename)
return relative_file
def FileLinkTuples(path : str, filename : str) -> list:
"""Returns a tuple of links from the file
Arguments :
path : str - The path to the filename. Might be relative path.
filename : str - The name of the html file to open and analyze.
Returns : list of tuples.
Each tuple has the 4 values.
HTMLFile - HTML file and path to the source file the link was found in.
fulllinkpath - Full path to the link on the system. Not a relative path.
linkpath - Path and name to the link specified. Relative address.
file exists - Boolean indicating if the file at the full link path exists.
Example
[ (('sample\\index.html', 'C:\\Website Analysis\\downloads.html', 'downloads.html', True)
]
"""
filelink_list = []
a = path.split('\\')
b = os.path.join(a[-1],filename)
c = os.path.realpath(filename)
if os.path.isfile(filename) == "True":
filelink_list.append((b,c,filename,"True"))
return filelink_list
Can someone please analyze my codes and tell me why the typles of (full_file,relative_file) and filelinks are not appending to the empty lists in the main program?
For this assignment, i was given a Unittest. The functions passed the test, but i don't know why the lists in the main program are not updating?
When i print the websitelink_list and htmlfiles, they are both empty.
What am i doing wrong with the def FileLinkTuples? particularly the
if os.path.isfile(filename) == "True":
filelink_list.append((b,c,filename,"True"))
Thanks
As far as I can tell, your code does produce nonempty results for htmlfiles at least. The main problem that I see is this:
if os.path.isfile(filename) == "True":
The function returns a Boolean, not a string, so you should check against the Boolean value True, or even omit it altogether:
if os.path.isfile(filename):
After fixing that, you should expect something in websitelink_list as well, provided your directory does in fact contain *.html files.
Now, if you are not seeing anything in htmlfiles at all (even if the directory does contain *.html files) then it must be something else that you're not showing here, since it appears to work correctly on my computer.

Find folder of the current javascript macro in iMacros?

I want to be able to reference the iMacros folder from my javascrip script in order to write:
retcode = iimPlay(folder + "/macro1.iim");
retcode = iimPlay(folder + "/macro2.iim");
instead of
retcode = iimPlay("test/macro1.iim");
retcode = iimPlay("test/macro2.iim");
I know it is possible in vbs but I don't know if that is the case in javascript.
it works in a similar way with javascript, here is example of the code:
var folder="c:\\data\\"
iimPlay(folder+"1.iim");
this code will run the 1.iim script from c:\data folder
You can make use of inbuilt !FOLDER_DATASOURCE variable to get folder of current javascript macro.
//Extract folder path of 'Datasources' folder (located inside 'iMacros' folder)
iimPlayCode("SET !EXTRACT {{!FOLDER_DATASOURCE}}");
var folderPath = iimGetExtract();
//Remove 'Datasources' from end of folder path string
folderPath = folderPath.slice(0,-11);
//Append 'Macros' to end of above path
folderPath = folderPath+"Macros\\";
alert(folderPath);
Performing above steps in single command.
//Extract folder path of 'Datasources' folder (located inside 'iMacros' folder)
iimPlayCode("SET !EXTRACT {{!FOLDER_DATASOURCE}}");
//Remove 'Datasources' from end of folder path string and append 'Macros'
var folderPath = iimGetExtract().slice(0,-11)+"Macros\\";
alert(folderPath);
Another possible approach is to replace 'Datasources' with 'Macros' in folder path.
//Extract folder path of 'Datasources' folder (located inside 'iMacros' folder)
iimPlayCode("SET !EXTRACT {{!FOLDER_DATASOURCE}}");
//Replace 'Datasources' with 'Macros' in folder path string
var folderPath = iimGetExtract().replace("Datasources","Macros\\");
alert(folderPath);
However it may create problem if folder path contains 'Datasources' anywhere else. You may use any of above methods as per your choice.
Once you get folder path of 'Macros' folder, you can use it like.
retcode = iimPlay(folderPath + "macro1.iim");

Get full directory contents with AppleScript

I need to get the entire (visible) contents of a folder and its subfolders as a list. Is this possible?
see how easy this can be
tell application "Finder"
set file_list to entire contents of (choose folder with prompt "Please select directory.")
end tell
if you want a list of file names then you could do this
tell application "Finder"
set file_list to name of every file of entire contents of (choose folder with prompt "Please select directory.")
end tell
Yes, entire contents does exactly what you say -- but it easily chokes on
large folders, and takes forever. It's OK for
small things, like extracting all the files of one kind out of a folder you
know will only contain a small number of files.
The recursive method also works well -- but it's using "list folder", and
the dictionary listing for it says it's deprecated and we shouldn't use it
any more.
I'm sure there is a shell command that can do this faster, but here is one way in pure Applescript that gives you total control over formatting what info you would like displayed.
property kFileList : {}
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my createList(source_folder)
end tell
on createList(item_list)
set the the_items to list folder item_list without invisibles
set item_list to item_list as string
repeat with i from 1 to number of items in the the_items
set the_item to item i of the the_items
set the_item to (item_list & the_item) as alias
set this_info to info for the_item
set file_name to name of this_info
set end of kFileList to file_name
if folder of this_info is true then
my createList(the_item)
end if
end repeat
end createList
On a side note, there are also a number file listing applications that can do this faster than Applescript.
UPDATE: As a result of this discussion, here is the function again, but this time using the updated API. This could probably could use some cleaning up, but it works cataloging my Desktop handily enough (and that's a deep, deep folder for me):
property kFileList : {}
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my createList(source_folder)
end tell
return kFileList
on createList(mSource_folder)
set item_list to ""
tell application "System Events"
set item_list to get the name of every disk item of mSource_folder
end tell
set item_count to (get count of items in item_list)
repeat with i from 1 to item_count
set the_properties to ""
set the_item to item i of the item_list
set the_item to ((mSource_folder & the_item) as string) as alias
tell application "System Events"
set file_info to get info for the_item
end tell
if visible of file_info is true then
set file_name to displayed name of file_info
set end of kFileList to file_name
if folder of file_info is true then
my createList(the_item)
end if
end if
end repeat
end createList
Wow this is quite late but I checked and it works.
tell application "Finder" to set folder_root to (choose folder with prompt "Please select directory.")
set fl to {}
dump_folder(folder_root)
on dump_folder(f)
global fl
tell application "System Events" to set end of fl to (get the POSIX path of f)
tell application "Finder" to set nfl to (the items of the contents of f)
repeat with nf in nfl
dump_folder(nf as alias)
end repeat
end dump_folder
fl