Verabeitung eines Ordners mit Unterordnern
Verfasst: Dienstag 3. Mai 2011, 16:23
Hallo,
im folgenden Listing ist der Rahmen für die Verarbeitung von Unterordnern vorgegeben:
da möchte ich folgende Syntax einsetzen:
mangels Python-Kenntnissen ist mir der Erfolg trotz stundenlangen Probierens versagt geblieben.
Kann jemand Helfen?
Danke
Hajo
im folgenden Listing ist der Rahmen für die Verarbeitung von Unterordnern vorgegeben:
Code: Alles auswählen
from JascApp import *
import os.path
import fnmatch
import sys
import glob
# Template script to process files in a given directory tree. You can specify
# the tree to search, the files to open, and whether they should be saved or not.
# The ProcessFile method is called for each file opened, and then the file is
# optionally saved
#
# Areas you are expected to modify are marked with ***
# *** Set this to the directory tree you want to process
DirectoryToUse = r'H:\Temp1'
# *** Include all of the extensions or patterns you care about - or just make it *.*
DirectorySearchString = [ '*.jpg', '*.PspImage', '*.png' ]
# *** Set this to true to search subdirectories of DirectoryToUse
# set it to false to search only in the specified directory
#SearchSubDirectories = App.Constants.Boolean.false
SearchSubDirectories = App.Constants.Boolean.true
# *** Set this value to true to save the file before closing it. If set to false the file
# is not saved, meaning that the ProcessFile method is responsible for doing something
# with the files
#SaveFileAfterProcessing = App.Constants.Boolean.false
SaveFilesAfterProcessing = App.Constants.Boolean.true
def ScriptProperties():
return {
'Author': 'Joe Fromm',
'Copyright': 'Copyright 2002 Jasc Software Inc., all rights reserved.',
'Description': 'Open and process an entire directory of files.',
'Host': 'Paint Shop Pro',
'Host Version': '8.00'
}
def ProcessFile( Environment, FileName ):
' *** Process a single file - put your code here'
print 'Processing File %s' % FileName
App.Do( Environment, 'Flip' )
def SearchDir( OutList, CurrentDir, FilesInDir ):
''' Called by os.path.walk for each directory we encounter. Gets passed the
list of files we are building up (OutList), the directory we are visiting
(CurrentDir), and a list of all the filenames (without path) in the current
directory. We need to strip out anything that is a directory or doesn't
match our search string.
'''
for File in FilesInDir:
for SearchPattern in DirectorySearchString:
if fnmatch.fnmatch( File, SearchPattern ):
FullPath = os.path.join( CurrentDir, File )
# make sure what we have is a file and not a subdirectory
if not os.path.isdir( FullPath ):
OutList.append( FullPath )
def Do(Environment):
# iterate through the search strings and glob all the results into one big list
CandidateFiles = [] # this will be a list of all the files we find
if SearchSubDirectories == App.Constants.Boolean.true:
# walk the directory tree, calling SearchDir on each directory visited.
os.path.walk( DirectoryToUse, SearchDir, CandidateFiles )
else:
# just glob the path provided rather than walk a tree
for Search in DirectorySearchString:
# concatenate the dir and file spec together
SearchPath = os.path.join( DirectoryToUse, Search )
GlobbedFiles = glob.glob( SearchPath ) # glob will return a list of files
for File in GlobbedFiles: # now iterate the list
CandidateFiles.append( File ) # and add each one to candidate list
# going to have a problem if there aren't any files.
if len(CandidateFiles) == 0:
print >>sys.stderr, "No files found to process"
return
# now process the list of files
for File in CandidateFiles:
print 'Opening file ', File
# open the file
try:
App.Do( Environment, 'FileOpen', {
'FileList': [ File ],
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Silent,
'AutoActionMode': App.Constants.AutoActionMode.Match
}
})
except:
print >>sys.stderr, 'Error opening file %s - skipping' % File
# for simplicity, place your code in the ProcessFile method
ProcessFile( Environment, File )
# save the file
try:
if SaveFilesAfterProcessing == App.Constants.Boolean.true:
App.Do( Environment, 'FileSave' )
# close the file before going on to the next
App.Do( Environment, 'FileClose' )
except:
print >>sys.stderr, 'Error on save/close of file %s - aborting' % FileName
da möchte ich folgende Syntax einsetzen:
Code: Alles auswählen
# Sample script using ResizeToLimit
from JascApp import *
def ScriptProperties():
return {
'Author': u'Gary Barton',
'Copyright': 'Copyright © 2003 Gary Barton.',
'Description': u'Resize image to given height and width max, while maintaining aspect ratio.',
'Host': u'Paint Shop Pro',
'Host Version': u'8.01'
}
def Do(Environment):
# Default values for maximum height and width (change these to use in batch)
# A value of 0 means the width or height will not be constrained.
# DIN A4:
width = 850
height = 850
# Prompt for height and width. This won't be visible if silent execution
# is set (such as in Batch). The defaults will be used instead.
result = App.Do( Environment, 'GetNumber', {
'DefaultValue': width,
'MinValue': 0,
'MaxValue': 65535,
'DialogTitle': 'GetNumber - ResizeToLimit',
'Prompt': 'Maximum width (enter 0 for none)',
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Default
}
})
if not result['OKButton']:
return
width = result['EnteredNumber']
result = App.Do( Environment, 'GetNumber', {
'DefaultValue': height,
'MinValue': 0,
'MaxValue': 65535,
'DialogTitle': 'GetNumber - ResizeToLimit',
'Prompt': 'Maximum height (enter 0 for none)',
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Default
}
})
if not result['OKButton']:
return
height = result['EnteredNumber']
# Do the resize with the specified maximum height and width
ResizeToLimit(Environment, Height=height, Width=width)
# ----- ResizeToLimit Start -----
# Author: Gary Barton
# Revision: 0.2
def ResizeToLimit(Environment, Height, Width):
'''Resize image to given height and width max, while maintaining aspect ratio.'''
# If width or height was given as zero then make them unlimited else...
# If preserving the aspect ratio would push the height over the limit for
# the specified width then set width to None (i.e. tell PSP to figure it
# out). Otherwise, let PSP to figure out the height.
if Width == 0 and Height == 0:
return
if Width == 0:
Width = None
elif Height == 0:
Height = None
elif ((float(Width) / App.TargetDocument.Width) * App.TargetDocument.Height) > Height:
Width = None
else:
Height = None
App.Do( Environment, 'Resize', {
'AspectRatio': None, # Let PSP figure it out
'Height': Height,
'Width': Width,
'Resolution': 72,
'CurrentDimensionUnits': App.Constants.UnitsOfMeasure.Pixels,
'CurrentResolutionUnits': App.Constants.ResolutionUnits.PixelsPerIn,
'MaintainAspectRatio': App.Constants.Boolean.true,
'Resample': App.Constants.Boolean.true,
'ResampleType': App.Constants.ResampleType.SmartSize,
'ResizeAllLayers': App.Constants.Boolean.true,
'GeneralSettings': {
'ExecutionMode': App.Constants.ExecutionMode.Silent,
'AutoActionMode': App.Constants.AutoActionMode.Match
}
})
# ----- ResizeToLimit End -----
mangels Python-Kenntnissen ist mir der Erfolg trotz stundenlangen Probierens versagt geblieben.
Kann jemand Helfen?
Danke
Hajo