Welcome! Log In Create A New Profile

Advanced

Slic3r droplet for OSX

Posted by Thinkyhead 
Slic3r droplet for OSX
May 02, 2013 05:57AM
I've been slicing up a lot of Mendel parts onto an SD card lately, and it's rather slow going through host software or the Slic3r application every time. Then I end up with gcode files that are hard to differentiate at a glance from others, so I always have to do some extra organizing work. The output_filename_format setting could help, but most host software overrides it.

To deal with these issues and speed things up I decided to make a droplet that would slice any number of files with one selected config file, then name the output files according to the config file's name. So, for example, I have a config file named "config-PLA-for-mendel.ini." If I select it as the config for slicing "y-idler.stl" I get the output file "y-idler-PLA-for-mendel.gcode," which is much easier to identify.

The droplet is attached, and here's the source code. Remember that if you change anything in the AppleScript droplet, you'll need to re-save the "slice.sh" shell script into its Resources folder in the app bundle. Or keep slice.sh wherever you prefer and just change the path in the droplet.

AppleScript Droplet Code

-- Slic3r batch script by Thinkyhead
-- Version 1.0 (May 2, 2013)
-- Drop an STL onto this, choose a config file, and wait

on open filelist
	set slic3rMac to path to application "Slic3r"
	set slic3rHome to cut_off(slic3rMac as string, ":")
	tell application "Slic3r" to quit
	
	set theConfig to choose file with prompt ¬
		"Choose a Slic3r config file" of type {"public.plain-text"} ¬
		default location (slic3rHome as alias) ¬
		with multiple selections allowed
	
	set sliceScript to POSIX path of (path to resource "slice.sh")
	
	repeat with stl in filelist
		do shell script sliceScript & " \"" & POSIX path of slic3rHome & "\" \"" & POSIX path of stl & "\" \"" & POSIX path of theConfig & "\""
	end repeat
	
	beep
	delay 3

	tell application "Slic3r" to quit

end open

on cut_off(the_string, delimiter)
	set old_delims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delimiter
	set temp_list to reverse of (every text item of the_string)
	try
		if item 1 of temp_list = "" then
			set temp_list to rest of temp_list
		end if
	end try
	if (count temp_list) < 2 then
		set AppleScript's text item delimiters to old_delims
		error "There is nothing to cut off in handler “cut_off”. String: “" & ¬
			the_string & "”, delimiter: “" & delimiter & "”."
	end if
	set temp_list to rest of temp_list
	set temp_list to reverse of temp_list
	-- next makes sure the delimiter as added at end
	copy "" to end of temp_list
	set r to (temp_list as text)
	set AppleScript's text item delimiters to old_delims
	return r
end cut_off

Helper Shell Script Code

#!/bin/bash
#
# slice.sh helper script
# Version 1.0 by Thinkyhead (May 2, 2013)
#
# This needs to live in DoSlice.app/Contents/Resources
#
SLICER_HOME=$1                                    
SLICER=$SLICER_HOME/Slic3r.app/Contents/MacOS/slic3r

if [ $# -lt 3 ]; then
  INCONFIG=$SLICER_HOME/config.ini
else
  INCONFIG=$3
fi

CONFIG=`basename "$INCONFIG" | sed -E 's/^config-?|\.ini$//g'`
OUTNAME=${2/.stl/}

if [ "$CONFIG" != "" ]; then
  OUTNAME="$OUTNAME-$CONFIG"
fi

$SLICER "$2" --load "$3" --output "$OUTNAME.gcode"

Edited 2 time(s). Last edit at 05/02/2013 06:02AM by Thinkyhead.


|
| Lead Developer of Marlin Firmware
| Help support my work at Patreon and Elsewhere.
|
Attachments:
open | download - DoSlice.zip (58.5 KB)
Re: Slic3r droplet for OSX
May 11, 2013 10:44PM
Who needs a helper script when you can call shell script by string directly in AppleScript? For this revision I removed dependency on an external shell script. I also employed a new trick to get the location of Slic3r.app (because AppleScript's "get path to application" causes Slic3r to launch). This method finds all your copies of Slic3r, including any that might be embedded in the Repetier Host app package, then it just assumes the shortest path is the right one.

Other than that the rest is pretty much the same, only AppleScript instead of BASH. I'll leave the previous version up for comparison.

You can keep up with development at [github.com]

-- Slic3r batch script by Thinkyhead
-- Version 1.1 (May 10, 2013)
-- Drop an STL onto this, choose a config file, and wait

on open fileList
  set slic3rMac to ApplicationAlias("Slic3r")
  set slic3rHome to posix_dirname(slic3rMac)
  set theConfig to choose file with prompt "Choose a Slic3r config file" of type {"public.plain-text"} default location (POSIX file slic3rHome) without invisibles
  set SLICER to (POSIX path of slic3rMac) & "Contents/MacOS/slic3r"
  set CONFIG to regex(posix_basename(theConfig), "^config-?|\\.ini$", "")
  set filesDone to 0
  set totalFiles to count fileList
  repeat with stl in fileList
    --set perc to (filesDone * 100 / totalFiles) as integer
    --display dialog "Slicing " & filesDone & " of " & totalFiles & " (" & perc & "%)" giving up after 1 with icon note
    set stlPath to POSIX path of stl
    if stlPath ends with ".stl" then
      set OUTNAME to regex(stlPath, "\\.stl", "")
      if not CONFIG = "" then set OUTNAME to OUTNAME & "-" & CONFIG
      
      do shell script quoted form of SLICER & ¬
        " " & quoted form of stlPath & ¬
        " --load " & quoted form of POSIX path of theConfig & ¬
        " --output " & quoted form of (OUTNAME & ".g")
      
      set filesDone to filesDone + 1
    end if
  end repeat
  
  beep
  set lingerTime to 5
  set timestamp1 to (do shell script "date +%s") as integer
  display dialog "Done slicing " & filesDone & " objects!" giving up after lingerTime with icon note
  set timestamp2 to (do shell script "date +%s") as integer
  set leftover to lingerTime - (timestamp2 - timestamp1)
  if leftover < 0.5 then set leftover to 0.5
  repeat leftover * 2 times
    if application "Slic3r" is running then
      tell application "Slic3r" to quit
      quit
    end if
    delay 0.5
  end repeat
  
end open

on ApplicationAlias(appName)
  set lsRegisterPath to "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
  set theAppPaths to paragraphs of (do shell script lsRegisterPath & " -dump | grep \"path:\" | grep \"" & appName & "\" | sed -E 's/.*path:[ ]+//g'")
  set shortestPath to ""
  repeat with appPath in theAppPaths
    if shortestPath is "" or appPath's length is less than shortestPath's length then
      set shortestPath to appPath as string
    end if
  end repeat
  return POSIX file shortestPath as alias
end ApplicationAlias

on regex(the_string, search_string, replace_string)
  return do shell script "echo " & quoted form of the_string & " | sed -E " & quoted form of ("s/" & search_string & "/" & replace_string & "/g")
end regex

on posix_basename(posix_path)
  return do shell script "basename " & quoted form of path_of(posix_path)
end posix_basename

on posix_dirname(posix_path)
  return do shell script "dirname " & quoted form of path_of(posix_path)
end posix_dirname

on path_of(path_or_item)
  if class of path_or_item is alias then
    return POSIX path of path_or_item
  end if
  return path_or_item
end path_of

Edited 3 time(s). Last edit at 05/12/2013 04:14AM by Thinkyhead.


|
| Lead Developer of Marlin Firmware
| Help support my work at Patreon and Elsewhere.
|
Attachments:
open | download - DoSlice.zip (251.5 KB)
Sorry, only registered users may post in this forum.

Click here to login