• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Export Handlers from Libraries
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Export Handlers from Libraries


  • Subject: Export Handlers from Libraries
  • From: Christopher Stone <email@hidden>
  • Date: Sat, 15 Feb 2014 23:08:46 -0600

Hey Folks,

Okay.  Here's the basic export handlers from libraries script.

I'm dumping each handler to a text file in the Finder named with the handler-name and then the library name in curly brackets.

Warning - subsequent runs will overwrite any existing files.

This one does not support script bundles, but that's not hard to fix.

It only exports bare handlers at present:

on handlerName(parameters)
script text
on handlerName

All comments within the handler-declaration are retained of course.

I will adjust mine to include all related comments outside the handler-declaration, but I need to look at how I've structured my libraries first and decided if I want to make any changes.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <email@hidden>
# dCre: 2014-02-15 : 22:00
# dMod: 2014-02-15 : 23:04
# Appl: Finder
# Task: Export all handlers from all libraries (except tagged Red) to 'Handler Library' folder.
# Libs: None
# Osax: Satimage.osax { http://tinyurl.com/dc3soh }
# Tags: @Applescript, @Script, @Finder, @Handler, @Export
-------------------------------------------------------------------------------------------
--» MAIN
-------------------------------------------------------------------------------------------

try

  

  set handlerLibFldr to ((path to documents folder as text) & "Scripts (Library):Handler Library:")
  set libFolder to alias ((path to library folder from user domain as text) & "Script Libraries:")

  

  tell application "Finder"
    set libList to (files of libFolder whose label index is not 2 and kind is "Compiled OSA Script") as alias list
  end tell

  

  if libList ≠ {} then

    

    if exTant(handlerLibFldr) = false then
      set pxPath to quoted form of (POSIX path of handlerLibFldr)
      do shell script "mkdir -p " & pxPath
    end if

    

    repeat with scriptFile in libList
      set fileName to cng("^.+:(.+)", " { \\1 }", (scriptFile as text)) of me
      set libSrc to load resource 2001 type "TEXT" from scriptFile as «class utf8»
      set handlerList to fnd("(?m)^on (\\w+)\\(.*\\)[^\\n\\r]*$.+^end \\1$", libSrc, 0, 1, 1) of me
      repeat with i in handlerList
        set handlerText to i's contents
        set handlerName to fnd("(?<=^on )\\w+", handlerText, 0, 0, 1) of me
        writeUTF8(handlerText, (handlerLibFldr & handlerName & fileName & ".txt"))
      end repeat
    end repeat

    

    tell application "Finder"
      activate
      if (window "Handler Library" exists) then
        set index of window "Handler Library" to 1
      else
        open alias handlerLibFldr
        set bounds of front window to {538, 44, 1382, 1196}
      end if
    end tell

    

  end if

  

on error e number n
  stdErr(e, n, true, true) of me
end try
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
  set e to e & return & return & "Num: " & n
  if beepFlag = true then
    beep
  end if
  if ddFlag = true then
    tell me
      set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
    end tell
    if button returned of dDlg = "Copy" then set the clipboard to e
  else
    return e
  end if
end stdErr
-------------------------------------------------------------------------------------------
on cng(findText, changeText, srcData)
  change findText into changeText in srcData with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on fnd(_find, _data, _case, _all, strRslt) # Last 3 are all bool
  try
    find text _find in _data case sensitive _case all occurrences _all string result strRslt with regexp
  on error
    return false
  end try
end fnd
-------------------------------------------------------------------------------------------
on exTant(_path) # Takes a path as input - HFS, Posix, or ~/Posix
  try
    if _path starts with "~/" then
      set _path to (POSIX path of (path to home folder as text)) & text 3 thru -1 of _path
    end if
    if _path contains ":" then
      alias _path
    else if _path contains "/" then
      alias POSIX file _path
    end if
    return true
  on error
    return false
  end try
end exTant
-------------------------------------------------------------------------------------------
on writeUTF8(_text, _file)
  try
    set fRef to open for access _file with write permission
    set eof of fRef to 0
    write _text to fRef as «class utf8»
    close access fRef
  on error e number n
    try
      close access fRef
    on error e number n
      error "Error in writeUTF8() handler!" & return & return & e
    end try
  end try
end writeUTF8
-------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: Upgrading to Mavericks - is it likely to break EVERYTHING!?
  • Next by Date: Re: Upgrading to Mavericks - is it likely to break EVERYTHING!?
  • Previous by thread: Re: Macro vs Script
  • Next by thread: Re: AppleScript-Users Digest, Vol 11, Issue 60
  • Index(es):
    • Date
    • Thread