• 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
Smart-File Unsaved Script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Smart-File Unsaved Script


  • Subject: Smart-File Unsaved Script
  • From: Christopher Stone <email@hidden>
  • Date: Thu, 03 Dec 2015 05:58:08 -0600

Hey Folks,

This is my preliminary effort to smart-file unsaved scripts, so I don't have to do it manually or save to the Desktop (and file later).

When I save scripts I usually drop the full header-text into it as in the appended script, or I use an abbreviated header like so:

-------------------------------------------------------------------------------------------
# dNam: Working Script Name
# dMod: 2015/12/03 05:44 
-------------------------------------------------------------------------------------------

dNam stands for development name.

I have scripts that create these headers for me and drop them into the correct place in Script Debugger.  You can work the selection in the Script Editor too:

tell application "Script Editor"
  tell document 1
    set selection to {0, 0}
    set contents of selection to "# I am nuts for using the Script Editor!" & return & return
  end tell
end tell

The File-It script looks for # dNam:<text> AND # Task:<text> OR # Appl:<text>.

If an Appl: is found then the script tries to file the script in that application's script library folder using the task description as the file name.

If dNam: is found then the script is filed in my working-scripts folder with the development name as the file name.

I need to make this more intelligent still, but it's saving me time and clutter already.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/11/30 04:57
# dMod: 2015/12/03 05:36
# Appl: Script Debugger
# Task: Save front script in ~/Documents/Working Items (Current)/Working ⇢ AppleScripts/<script-name>
#     : OR
#     : Save front script in ~/Documents/Scripts (Library)/Application Scripting/<Appl-Folder>/<script-name>
# Osax: Satimage.osax { http://tinyurl.com/dc3soh }
# Tags: @Applescript, @Script, @Script_Debugger, @Smart, @Save
-------------------------------------------------------------------------------------------

try

  

  set myPath to (path to me as text)
  if myPath ends with "Script Debugger.app:" then error "Script is not saved!"

  

  set workingItemsFolderHFS to ((path to documents folder as text) & "Working Items (Current):Working ⇢ AppleScripts:")
  set applicationScriptingLibraryHFS to ((path to documents folder as text) & "Scripts (Library):Application Scripting:")

  

  tell application "Script Debugger"

    

    # Make sure NOT to use this script as its own target.
    if myPath = (get file spec of front document as text) then
      set scriptDoc to a reference to document 2
    else
      set scriptDoc to a reference to document 1
    end if

    

    if scriptDoc exists then
      tell scriptDoc
        set scriptSrc to its source text

        

        if fndBool("(?<=^# Appl: ).+", scriptSrc, false, false) of me then
          set destinationFolderPath to applicationScriptingLibraryHFS & (get matchResult of result) & ":"
          if not exTant(destinationFolderPath) of me then
            do shell script "mkdir -p " & (quoted form of (POSIX path of destinationFolderPath))
          end if
          set scriptName to fnd("(?<=^# Task: ).+", scriptSrc, false, true) of me
          set scriptName to cng("[[:punct:][:blank:]]+$", "", scriptName) of me
          set scriptName to cng(":", ";", scriptName) of me
          set savePath to destinationFolderPath & scriptName & ".scptd"

          

        else if fndBool("(?<=^# dNam: ).+", scriptSrc, false, false) of me then
          set scriptName to matchResult of result
          if scriptName = false then set scriptName to its name
          set scriptName to cng("^(.+)\\.scptd?$", "\\1", scriptName) of me
          set scriptName to cng("[[:punct:][:blank:]]+$", "", scriptName) of me
          set scriptName to cng(":", ";", scriptName) of me
          set savePath to workingItemsFolderHFS & scriptName & ".scptd"

          

        else

          

          error "No naming criteria found!"

          

        end if

        

        save it in savePath as bundled compiled script

        

      end tell
    end if

    

  end tell

  

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(_find, _replace, _data)
  change _find into _replace in _data with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on fnd(_find, _data, _all, strRslt)
  try
    find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
  on error
    return false
  end try
end fnd
-------------------------------------------------------------------------------------------
on fndBool(_find, _data, _all, strRslt)
  try
    find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
    return true
  on error
    return false
  end try
end fndBool
-------------------------------------------------------------------------------------------
on exTant(_path) # Takes an HFS, Posix, or ~/Posix path as input.
  try

    

    if _path is "~" or _path is "~/" then
      set _path to (POSIX path of (path to home folder as text))

      

    else 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 starts with "/" then
      alias POSIX file _path

      

    else
      alias _path

      

    end if

    

    return true

    

  on error
    return false

    

  end try
end exTant
-------------------------------------------------------------------------------------------

 _______________________________________________
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

  • Follow-Ups:
    • Re: Smart-File Unsaved Script
      • From: Christopher Stone <email@hidden>
  • Prev by Date: Safari – Duplicate Tab Next to Itself
  • Next by Date: Re: Smart-File Unsaved Script
  • Previous by thread: Safari – Duplicate Tab Next to Itself
  • Next by thread: Re: Smart-File Unsaved Script
  • Index(es):
    • Date
    • Thread