Farbflash projects: Imaging lingo table | 3-D scene list | Find all | Handler menu | Lingo message window

source: trunk/lingosource/castlib2/FileIOFunktionen.ls

Last change on this file was 245, checked in by alex, 2 years ago

cleanup

File size: 29.5 KB
Line 
1-- FileIOFunktionen
2-----------------------------------
3-- CREATED:
4-- c03 Alex da Franca -- alex@farbflash.de
5-- PROPERTIES:
6--!memberProperties: [#name: "FileIOFunktionen", #scripttype: #parent, #scriptSyntax: #lingo, #comments: "~/Documents/Scripts/lingo/FileIOFunktionen.ls"]
7--
8-- DESCRIPTION: FileIO Handlers
9-- -
10--
11-- REQUIRES:
12-- (Prerequisites)
13--
14-- USAGE:
15-- -
16--
17-- EXAMPLE:
18-- -
19-----------------------------------
20
21on _____________________PROPERTY_DECLARATION me
22end
23-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
24property ancestor
25
26-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
27on ___________________STANDARD_EVENTS me
28end
29-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
30
31on new me
32  Script_Root_Object = member("Script_Root_Object")
33  if ilk(Script_Root_Object) = #member then
34    if Script_Root_Object.type = #script then
35      ancestor = new(script "Script_Root_Object")
36      mSetScriptName me, "FileIOFunktionen"
37    end if
38  end if
39  return me
40end
41
42-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
43on ___________________PUBLIC_EVENTS me
44end
45-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
46
47
48-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
49-- xxxxxxxxxxxxxxxxxx Read text in (if no path is provided show open dialog)
50-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
51
52on mGetTextFromFile me, pfad, readByteArray, useCharSet
53  retval = ""
54  if the runmode contains "Plugin" then return retval
55 
56  fio = new(xtra "fileio")
57  if not objectP(fio) then return retval
58 
59  useCharSet = string(useCharSet)
60  if length(useCharSet) > 0 then
61    if xscr().mGetVersionNumber() > 11.4 then
62      ------------- don't need to check whether the given charset is supported
63      ------------- as it is simply ignored, if it isn't
64      ------------- and it also gets reset so that the next instance of fileio
65      ------------- uses the default utf-8 again
66      --      installedCharSets = _system.getInstalledCharSets()
67      --      if installedCharSets.getPos(useCharSet) > 0 then fio.setCharSet(useCharSet)
68      fio.setCharSet(useCharSet)
69    end if
70  end if
71 
72  if (voidP(pfad) or (pfad = "")) then pfad = mGetFilePathFromUser(me)
73 
74  if ilk(pfad) = #string then
75    if pfad.length > 0 then
76      fio.openFile(pfad, 1)
77      if fio.status() = 0 then
78        if readByteArray = 1 then retval = fio.readByteArray(fio.getlength())
79        else retval = fio.readFile()
80        fio.closeFile()
81      end if
82    end if
83  end if
84  fio = void
85  return retval
86end
87
88-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
89
90on mGetByteArrayFromFile me, thePath
91  return mGetTextFromFile(me, thePath, (xscr().mGetVersionNumber() > 11.49))
92end
93
94-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
95-- xxxxxxxxxxxxxxxxxx Read text asynchron -> works in projector, authoring AND shockwave
96-- requires parentscript "NetTextReaderPS"
97-- example:
98
99-- paramList = [:]
100-- paramList[#theURL] = mGetRelativePath("someFile.txt") -- "http://www.ff.com/somefile.txt" -- ABSOLUTE PATH !!
101-- paramList[#handler] = #callbackFromGetText
102-- paramList[#target] = instance
103-- -- optional:
104-- paramList[#theTimeOut] = 4000 -- milliseconds until operation times out
105-- paramList[#progressHandler] = #progresscallbackFromGetText -- event sent whenever the percent done changes
106-- paramList[#isByteArray] = 0 -- boolean value, only D11
107-- paramList[#charset] = "windows-1252" -- string, only D11
108-- -- paramList[#customdata] = "whatever you need"
109-- xscr(#FileIOFunktionen).mGetTextFromFileAsync(paramList)
110
111--
112--on callbackFromGetText me, paramList
113--  theTextResult = string(paramList.getaprop(#theTextResult))
114--  if length(theTextResult) > 0 then
115--    -- do something with theTextResult
116--  else
117--    alert string(paramList.getaprop(#theNetError)
118--  end if
119--end
120
121
122on mGetTextFromFileAsync me, paramList
123  fname = string(paramList.getaprop(#theURL))
124  internetLink = (offset("://", fname) > 0)
125  if not(internetLink) then internetLink = (fname starts "www.")
126  if the runmode contains "plugin" or internetLink then
127   
128    --    scr = xscr().mGetInstance("NetLingoManagerPS")
129    --    if ilk(scr) = #instance then
130    --      scr.mStartNetOP(paramList)
131    --    else
132      scr = xscr().mGetInstance("NetTextReaderPS")
133    if ilk(scr) = #instance then
134      scr.mAddJob(paramList)
135    else
136      paramList.setaprop(#theTextResult, "")
137      paramList.setaprop(#theNetError, "Script NetTextReaderPS not found")
138      call(paramList.getaprop(#handler), [paramList.getaprop(#target)], paramList)
139    end if
140    --    end if
141  else
142    paramList.setaprop(#theTextResult, mGetTextFromFile(me, paramList.getaprop(#theURL), paramList[#isByteArray], paramList[#charset]))
143    theResult = xscr().mDoDelayedCallback(paramList)
144    if theResult <> 1 then call(paramList.getaprop(#handler), [paramList.getaprop(#target)], paramList)
145  end if
146end
147
148-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
149
150on mGetByteArrayFromFileAsync me, paramList
151  paramList[#isByteArray] = 1
152  mGetTextFromFileAsync me, paramList
153end
154
155-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
156-- xxxxxxxxxxxxxxxxxx Write text out to a file
157-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
158
159on mSaveToTextFile me, theText, pfad, theTitle, theDefaultName, startfolder, useCharSet
160  retval = 0
161 
162  if ilk(theText) <> #string then return retval
163 
164  if the runmode contains "plugin" then
165    if ilk(theDefaultName) <> #string then theDefaultName = "Untitled"
166    gotonetpage("mailto:alex@farbflash.de?subject="&theDefaultName&"&body="&urlencode(theText))
167    return 1
168  else
169    fio = new(xtra "fileio")
170    if not objectP(fio) then return retval
171   
172    useCharSet = string(useCharSet)
173    if length(useCharSet) > 0 then
174      if xscr().mGetVersionNumber() > 11.4 then
175        ------------- don't need to check whether the given charset is supported
176        ------------- as it is simply ignored, if it isn't
177        ------------- and it also gets reset so that the next instance of fileio
178        ------------- uses the default utf-8 again
179        --      installedCharSets = _system.getInstalledCharSets()
180        --      if installedCharSets.getPos(useCharSet) > 0 then fio.setCharSet(useCharSet)
181        fio.setCharSet(useCharSet)
182      end if
183    end if
184   
185   
186    if ilk(theTitle) <> #string then theTitle = "Save File"
187   
188    if ilk(theDefaultName) <> #string then theDefaultName = "Untitled"
189   
190    if (voidP(pfad) or (pfad = "")) then
191      if call(#mCheckForXtra, mGetXScript(), "BudAPI") = 1 then
192        startfolder = string(startfolder)
193       
194        resetUNames = 0
195        if the platform contains "mac" then
196        if baSysFolder("prefs") starts "/" then
197          baReturnUnixNames(0)
198          resetUNames = 1
199        end if
200        end if
201        if length(startfolder) > 0 then
202          pfad = baGetFilename("save", startfolder, theDefaultName, "", 524288+2+4+4194304, theTitle, false, -1, -1)
203        else
204        pfad = baGetFilename("save", "", theDefaultName, "", 524288+2+4+4194304, theTitle, false, -1, -1)
205        end if
206       
207        if resetUNames = 1 then baReturnUnixNames(1)
208       
209      else
210        pfad = fio.displaySave(theTitle, theDefaultName)
211      end if
212    end if
213   
214    if ilk(pfad) = #string then
215      if pfad.length > 0 then
216       
217        fio.openFile(pfad, 0)
218        if fio.status() = 0 then fio.delete()
219       
220        fio.createFile(pfad)
221        if fio.status() = 0 then
222         
223          fio.openFile(pfad, 2)
224          if fio.status() = 0 then
225           
226            fio.writeString(theText)
227           
228            --            retval = (fio.status() = 0)
229            if fio.status() = 0 then retval = pfad
230           
231            fio.closeFile()
232          end if
233        end if
234      end if
235    end if
236   
237    fio = 0
238   
239  end if
240 
241  return retval
242end
243
244
245-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
246-- xxxxxxxxxxxxxxxxxx append string to file
247-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
248
249
250on mAppendTextToFile me, theText, pfad
251  retval = 0
252 
253  if ilk(theText) <> #string then return retval
254 
255  pfad = string(pfad)
256 
257  olddelim = the itemdelimiter
258  the itemdelimiter = the last char of the moviepath
259  fname = the last item of pfad
260  the itemdelimiter = olddelim
261 
262  if the runmode contains "plugin" then
263    if not(fname.length) then fname = "Untitled.txt"
264    altertext = getpref(fname)
265    if voidP(altertext) then altertext = theText
266    else put RETURN & theText after altertext
267    setpref(fname, altertext)
268    return 1
269  else
270   
271    fio = new(xtra "fileio")
272    if not objectP(fio) then return retval
273   
274    if pfad.length then
275     
276      if not xscr().mCheckFileExists(pfad) then
277        if xscr().mCheckFileExists(the moviepath & pfad) then
278          fio.createFile(the moviepath & fname)
279        else
280          theText = RETURN & theText
281        end if
282        fio.openFile(the moviepath & pfad, 0)
283      else
284       
285        theText = RETURN & theText
286        fio.openFile(pfad, 0)
287      end if
288     
289    else
290      pfad = the moviepath & the moviename & "_log.txt"
291     
292      if not xscr().mCheckFileExists(pfad) then fio.createFile(pfad)
293      else theText = RETURN & theText
294     
295      fio.openFile(pfad, 0)
296    end if
297   
298    if fio.status() <> 0 then
299      fio = 0
300      return retval
301    end if
302   
303    fio.setPosition(fio.getLength())
304   
305    fio.writeString(theText)
306   
307    retval = (fio.status() = 0)
308   
309    fio.closeFile()
310    fio = 0
311   
312  end if
313 
314  return retval
315end
316
317-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
318
319on mExportSelectedText me, theType
320 
321  cl = the activecastlib
322  sel = the selection of castlib cl
323  if not sel.count then exit
324  memref = member(sel[1][1], cl)
325  if [#text, #field].getPos(memref.type) < 1 then exit
326 
327  theDefaultName = memref.name
328 
329  theType = string(theType)
330 
331  if length(theType) < 1 then
332    if offset(".", theDefaultName) > 1 then
333      theType = theDefaultName.char[offset(".", theDefaultName) + 1 .. length(theDefaultName)]
334    end if
335  end if
336 
337  if memref.type = #field then theType = "txt"
338 
339  if ["txt", "rtf", "htm", "html"].getPos(theType) < 1 then theType = "txt"
340 
341 
342  if theDefaultName = "" then theDefaultName = "Untitled." & theType
343  if offset(".", theDefaultName) < 1 then put "." & theType after theDefaultName
344 
345  case theType of
346    "rtf":
347      derText = memref.rtf
348    "htm", "html":
349      derText = memref.html
350    otherwise:
351      derText = memref.text
352  end case
353 
354  mSaveToTextFile me, derText, "", "Save file", theDefaultName
355 
356end
357
358-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
359
360on mGetPathNameFromUser me
361  return mGetFilePathFromUser(me)
362end
363
364-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
365
366on mGetFilePathFromUser me, startDir, defaultName, filetypes, prompt
367  -----------------------------------
368  -- CREATED: -
369  -- ACTION: Asks the user for a path to a file and returns the filepath
370  -- INPUT: <startDir> ; string ; the directory to start the file selection dialog (only if buddyApi xtra is installed)
371  --        <defaultName> ; string ; pre-entered filename (only if buddyApi xtra is installed)
372  --        <filetypes> ; string ; filetype filter (only if buddyApi xtra is installed)
373  --        <prompt> ; string ; Caption of the dialog (only if buddyApi xtra is installed)
374  -- RETURNS: string ; absolute pathname delimited by the pathdelimiter of the current platform (widnows: "\", mac: ":") or "", if user canceled the process
375  -- EXAMPLE: relPath = xscr(#FileIOFunktionen).mGetFilePathFromUser(the moviepath, "soundRadius.xml", ".xml", "Please select soundradius scriptsettings")
376  -----------------------------------
377 
378  retval = ""
379 
380  if the runmode contains "Plugin" then return retval
381 
382  if call(#mCheckForXtra, mGetXScript(), "BudAPI") = 1 then
383   
384    resetUNames = 0
385    if the platform contains "mac" then
386    if baSysFolder("prefs") starts "/" then
387      baReturnUnixNames(0)
388      resetUNames = 1
389    end if
390    end if
391   
392    startDir = string(startDir)
393    defaultName = string(defaultName)
394    filetypes = string(filetypes)
395    if length(filetypes) < 1 then filetypes = "*.*"
396    prompt = string(prompt)
397   
398    retval = baGetFilename("open", startDir, defaultName, filetypes, 524288+4+2+4194304, prompt, false, -2, -1)
399   
400    if resetUNames = 1 then baReturnUnixNames(1)
401   
402    return retval
403   
404  else
405   
406   
407    fio = new(xtra "fileio")
408   
409    if not objectP(fio) then return retval
410   
411    retval = fio.displayOpen()
412   
413    fio = void
414   
415  end if
416 
417  return retval
418end
419
420-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
421
422on mGetMovieRelativePathFromUser me, startDir, defaultName, filetypes, prompt
423  -----------------------------------
424  -- CREATED: 14.12.2009
425  -- ACTION: Asks the user for a path to a file
426  --         Takes care, that the file starts with the moviepath => is WITHIN the moviepath,
427  --         otherwise it calls itself until either the user selects a relative file or cancels the process.
428  --         Converts path delimiters to "/" => normalize the pathdelimiter for both platforms
429  -- INPUT: <startDir> ; string ; the directory to start the file selection dialog (only if buddyApi xtra is installed)
430  --        <defaultName> ; string ; pre-entered filename (only if buddyApi xtra is installed)
431  --        <filetypes> ; string ; filetype filter (only if buddyApi xtra is installed)
432  --                               SPECIAL VALUE: can be "folder", which would not return a path to a folder,
433  --                               but rather a path to a folder
434  --        <prompt> ; string ; Caption of the dialog (only if buddyApi xtra is installed)
435  -- RETURNS: string ; relative pathname delimited by "/" or "", if user canceled the process
436  -- EXAMPLE: relPath = xscr(#FileIOFunktionen).mGetMovieRelativePathFromUser(the moviepath, "soundRadius.xml", ".xml", "Please select soundradius scriptsettings")
437  -----------------------------------
438 
439  if filetypes = "folder" then
440    searchItem = "folder"
441    thePath = mGetFolderPathFromUser(me)
442  else
443    searchItem = "file"
444    thePath = mGetFilePathFromUser(me, startDir, defaultName, filetypes, prompt)
445  end if
446  if length(thePath) > 0 then
447    if offset(the moviepath, thePath) <> 1 then
448      alert "This " & searchItem & " is NOT within the moviepath. Please choose a " & searchItem & ", which is inside the current movie's path!"
449      return mGetMovieRelativePathFromUser(me, startDir, defaultName, filetypes, prompt)
450    end if
451    delete char 1 to length(the moviepath) of thePath
452    delim = the last char of the moviepath
453    if delim <> "/" then
454      offs = offset(delim, thePath)
455      repeat while offs > 0
456        put "/" into char offs of thePath
457        offs = offset(delim, thePath)
458      end repeat
459    end if
460    if filetypes = "folder" then
461      if the last char of thePath <> "/" then put "/" after thePath
462      end if
463    end if
464    return thePath
465end
466
467-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
468
469on mGetFolderPathFromUser me
470  retval = ""
471 
472  if the runmode contains "Plugin" then return retval
473 
474  if xscr().mCheckForXtra("BudAPI") = 1 then -- if buddy is present...
475   
476    resetUNames = 0
477    if the platform contains "mac" then
478    if baSysFolder("prefs") starts "/" then
479      baReturnUnixNames(0)
480      resetUNames = 1
481    end if
482    end if
483   
484    retval = baGetFolder( "", "Select a folder", 3, "", 50, 50 )
485   
486    if resetUNames = 1 then baReturnUnixNames(1)
487   
488  else if xscr().mCheckForXtra("FileXtra4") = 1 then -- if not buddy then try filextra
489    fx = (xtra "filextra4").new()
490    if objectP(fx) then
491      retval = fx.fx_FolderSelectDialog("")
492    end if
493    fx = 0
494  else
495    fio = new(xtra "fileio")
496    if not objectP(fio) then return retval
497    retval = fio.displayOpen()
498    if length(retval) > 0 then
499      olddelim = the itemdelimiter
500      the itemdelimiter = the last char of the moviepath
501      delete the last item of retval
502      put the itemdelimiter after retval
503      the itemdelimiter = olddelim
504    end if
505    fio = void
506  end if
507 
508  return retval
509end
510
511-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
512
513on mGetFileList me, folderpath
514  if the runmode contains "plugin" then return []
515 
516  if length(string(folderpath)) < 1 then folderpath = mGetFolderPathFromUser(me)
517  if length(folderpath) < 1 then return []
518 
519  if xscr().mCheckForXtra("BudAPI") then -- if buddy is present...
520   
521    --  -- budApi version:
522    retval = baFileList(folderpath, "*.*")
523   
524  else if xscr().mCheckForXtra("FileXtra4") then -- if not buddy then try filextra
525    --  -- filextra4 version:
526    fx = (xtra "filextra4").new()
527    if objectP(fx) then
528      retval = fx.fx_FolderToList(folderpath)
529      delim = the last char of the moviepath
530      if length(delim) < 1 then
531        if not(the runmode contains "plug") then
532          delim = the last char of the applicationpath
533        end if
534      end if
535      if length(delim) < 1 then delim = "/"
536      repeat with n = count(retval) down to 1
537        if the last char of retval[n] = delim then retval.deleteAt(n)
538      end repeat
539    end if
540    fx = 0
541   
542  else
543   
544    retval = []
545    n = 1
546    this = getnthfilenameinfolder(folderpath, n)
547    repeat while length(this) > 0
548      if length(getnthfilenameinfolder(folderpath & this, 1)) = 0 then retval.add(this)
549      n = n + 1
550      this = getnthfilenameinfolder(folderpath, n)
551    end repeat
552  end if
553 
554  return retval
555end
556
557-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
558
559on mGetFolderList me, folderpath
560  if the runmode contains "plugin" then return []
561 
562  if length(string(folderpath)) < 1 then folderpath = mGetFolderPathFromUser(me)
563  if length(folderpath) < 1 then return []
564 
565  if xscr().mCheckForXtra("BudAPI") then -- if buddy is present...
566    --  -- budApi version:
567    retval = baFolderList(folderpath)
568   
569  else if xscr().mCheckForXtra("FileXtra4") then -- if not buddy then try filextra
570    --  -- filextra4 version:
571    fx = (xtra "filextra4").new()
572    if objectP(fx) then
573      retval = fx.fx_FolderToList(folderpath)
574      delim = the last char of the moviepath
575      if length(delim) < 1 then
576        if not(the runmode contains "plug") then
577          delim = the last char of the applicationpath
578        end if
579      end if
580      if length(delim) < 1 then delim = "/"
581      repeat with n = count(retval) down to 1
582       
583        if the last char of retval[n] <> delim then
584          retval.deleteAt(n)
585        else
586          str = retval[n]
587          --          delete the last char of str
588          retval[n] = str
589        end if
590       
591      end repeat
592    end if
593    fx = 0
594   
595  else
596   
597    retval = []
598    n = 1
599    this = getnthfilenameinfolder(folderpath, n)
600    repeat while length(this) > 0
601      if length(getnthfilenameinfolder(folderpath & this, 1)) > 0 then retval.add(this)
602      n = n + 1
603      this = getnthfilenameinfolder(folderpath, n)
604    end repeat
605  end if
606 
607  return retval
608 
609end
610
611
612-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
613
614on mGetFileAndFolderList me, folderpath
615  if the runmode contains "plugin" then return []
616 
617  if length(string(folderpath)) < 1 then folderpath = mGetFolderPathFromUser(me)
618  if length(folderpath) < 1 then return []
619 
620  if xscr().mCheckForXtra("BudAPI") then -- if buddy is present...
621    --  -- budApi version:
622    retval = baFolderList(folderpath)
623    filelist = baFileList(folderpath, "*.*")
624    repeat with f in filelist
625      retval.add(f)
626    end repeat
627   
628  else if xscr().mCheckForXtra("FileXtra4") then -- if not buddy then try filextra
629    --  -- filextra4 version:
630    fx = (xtra "filextra4").new()
631    if objectP(fx) then
632      retval = fx.fx_FolderToList(folderpath)
633    end if
634    fx = 0
635   
636  else
637   
638    retval = []
639    n = 1
640    this = getnthfilenameinfolder(folderpath, n)
641    repeat while length(this) > 0
642      retval.add(this)
643      n = n + 1
644      this = getnthfilenameinfolder(folderpath, n)
645    end repeat
646   
647  end if
648 
649  return retval
650 
651end
652
653
654
655-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
656-- xxxxxxxxxxxxxxxxxx Export BMP with FileIO Xtra
657-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
658
659on mExportBitmapFIO me, imageObj, pfadname
660  retval = 0
661  --  statusmessage = "Compiling bitmap data"
662 
663  -- Initialize byteList, get image information from cast member
664  byteList = []
665 
666  --  imageMember = member("sample")
667  --  imageObj    = imageMember.image
668 
669  if ilk(imageObj) <> #image then return retval
670 
671  imageWidth  = imageObj.width
672  imageHeight = imageObj.height
673  bitmapSize  = imageWidth*imageHeight*3
674 
675  if bitmapSize = 0 then return retval
676 
677  -- Add bitmap file header information
678  headerSize = 54
679  headLength = headerSize - 14
680  fileSize   = headerSize + bitmapSize + 0
681  byteList = appendByteData(me, byteList, #string, "BM")        -- Bitmap type
682  byteList = appendByteData(me, byteList, #uint32, fileSize)    -- File size
683  byteList = appendByteData(me, byteList, #uint32, 0)           -- Unused
684  byteList = appendByteData(me, byteList, #uint32, headerSize)  -- Offset to bitmap data
685  byteList = appendByteData(me, byteList, #uint32, headLength)  -- Length of file header (minus previous bytes)
686  byteList = appendByteData(me, byteList, #uint32, imageWidth)  -- Bitmap width
687  byteList = appendByteData(me, byteList, #uint32, imageHeight) -- Bitmap height
688  byteList = appendByteData(me, byteList, #uint16, 1)           -- Number of bitmap "planes"
689  byteList = appendByteData(me, byteList, #uint16, 24)          -- Bits per pixel
690  byteList = appendByteData(me, byteList, #uint32, 0)           -- Compression type
691  byteList = appendByteData(me, byteList, #uint32, 0)           -- Bitmap data size
692  byteList = appendByteData(me, byteList, #uint32, 0)           -- Horizontal resolution (not required)
693  byteList = appendByteData(me, byteList, #uint32, 0)           -- Vertical resolution (not required)
694  byteList = appendByteData(me, byteList, #uint32, 0)           -- Color count
695  byteList = appendByteData(me, byteList, #uint32, 0)           -- Important color count
696 
697  -- The # of bytes for each row of pixels must be divisible by 4, so each line is padded with 0 to 3 bytes
698  padBytes = integer((32 - ((imageWidth*3*8) mod 32))/8) mod 4
699 
700  -- Add bitmap image data
701  repeat with y = imageHeight-1 down to 0
702    repeat with x = 0 to imageWidth-1
703      pixel = imageObj.getPixel(x, y)
704      byteList.append(pixel.blue)
705      byteList.append(pixel.green)
706      byteList.append(pixel.red)
707    end repeat
708    repeat with padNum = 1 to padBytes
709      byteList.append(0)
710    end repeat
711  end repeat
712 
713  --  statusmessage = "Saving file"
714 
715  -- Save the image
716  retval = writeBinaryFile(me, pfadname, byteList, 1, 1, 1)
717 
718  --  statusmessage = "File saved"
719  return retval
720end
721
722-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
723--
724-- Converts lingo variables into the specified data type and appends the
725-- resulting byte values to the linear list named "byteList"
726--
727on appendByteData me, byteList, dataType, dataValue
728  if byteList.listP and dataType.symbolP then
729    case datatype of
730      #string:
731        if dataValue.stringP then
732          repeat with index = 1 to dataValue.length
733            byteList.append(charToNum(dataValue.char[index]))
734          end repeat
735        end if
736       
737      otherwise:
738        numBytes = 0
739        listlength = byteList.count
740       
741        dataTypes = [#uint16:2, #uint32:4]
742        if dataTypes.findPos(dataType) > 0 then numBytes = dataTypes.getAProp(dataType)
743       
744        repeat with index = numBytes down to 1
745          divisor = integer(power(2,((index-1)*8)))
746          result  = dataValue/divisor
747          dataValue = dataValue - divisor*result
748          byteList[listLength+index] = result
749        end repeat
750    end case
751  end if
752 
753  return byteList
754end
755
756-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
757--
758-- Uses FileIO to write the values of the byteList linear list into the file specified
759-- by "filePath" with options for selecting a start position in the file and also
760--
761on writeBinaryFile me, filePath, byteList, fileStartByte, listStartByte, deleteOriginalFile
762  retval = 0
763 
764  fio = new(xtra "fileIO")
765 
766  if ilk(filePath) <> #string then filePath = fio.displaySave("Save Bitmap:","Untitled.bmp")
767  if not(filePath.length) then return retval
768 
769  if deleteOriginalFile then
770    fio.openFile(filePath, 0)
771    if fio.status() = 0 then fio.delete()
772    fio.createFile(filePath)
773  end if
774  fio.openFile(filePath, 2)
775 
776  if fio.status() <> 0 then
777    fio.createFile(filePath)
778    fio.openFile(filePath, 2)
779  end if
780 
781  if fio.status() = 0 then
782    listLength = byteList.count
783    if fileStartByte.voidP then fileStartByte = 1
784    if listStartByte.voidP then listStartByte = 1
785    fio.setPosition(fileStartByte-1)
786   
787    repeat with index = listStartByte to listLength
788      fio.writeChar(numToChar(byteList[index]))
789    end repeat
790    retval = 1
791  end if
792 
793  fio.closeFile()
794  fio = 0
795  return retval
796end
797
798-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
799
800on interface me
801  str = "FileIO Scripts  by alex da franca c2003 -- alex@farbflash.de -- all rigths reserved"
802 
803  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
804  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
805  put RETURN & "on mGetTextFromFile me, pfad" after str
806  put RETURN & "" after str
807 
808  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
809  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
810  put RETURN & "on mGetTextFromFileAsync me, paramList" after str
811  put RETURN & "" after str
812 
813  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
814  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
815  put RETURN & "on mSaveToTextFile me, theText, pfad, theTitle, theDefaultName" after str
816  put RETURN & "" after str
817 
818  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
819  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
820  put RETURN & "on mAppendTextToFile me, theText, pfad" after str
821  put RETURN & "" after str
822 
823  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
824  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
825  put RETURN & "on mExportSelectedText me, theType" after str
826  put RETURN & "" after str
827 
828  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
829  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
830  put RETURN & "on mGetPathNameFromUser me" after str
831  put RETURN & "" after str
832 
833  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
834  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
835  put RETURN & "on mGetFilePathFromUser me" after str
836  put RETURN & "" after str
837 
838  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
839  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
840  put RETURN & "on mGetFolderPathFromUser me" after str
841  put RETURN & "" after str
842 
843  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
844  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
845  put RETURN & "on mGetFileList me, folderpath" after str
846  put RETURN & "" after str
847 
848  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
849  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
850  put RETURN & "on mGetFolderList me, folderpath" after str
851  put RETURN & "" after str
852 
853  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
854  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
855  put RETURN & "on mExportBitmapFIO me, imageObj, pfadname" after str
856  put RETURN & "" after str
857 
858  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
859  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
860  put RETURN & "on appendByteData me, byteList, dataType, dataValue" after str
861  put RETURN & "" after str
862 
863  put RETURN & "-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" after str
864  put RETURN & "-- xxxxxxxxxxxxxxxxxx Description" after str
865  put RETURN & "on writeBinaryFile me, filePath, byteList, fileStartByte, listStartByte, deleteOriginalFile" after str
866  put RETURN & "" after str
867  return str
868end
Note: See TracBrowser for help on using the repository browser.