Turn CSS into Stylus

Hello!

Trying to convert a selected style.css file into a style.styl file. I have installed NodeJS and css2stylus.
Should be quite simple? The command in Terminal is:

css2stylus path/to/style.css

I read from this post on how to add the $PATH variable. Can get connected through KM with this simple path:

css2stylus -v

Got any ideas what I am doing wrong? I keep on getting this error:

/usr/local/lib/node_modules/css2stylus/bin/css2stylus:97
throw new Error('Input file must be css!');
^

Error: Input file must be css!
at /usr/local/lib/node_modules/css2stylus/bin/css2stylus:97:11
at Array.forEach (native)
at Object. (/usr/local/lib/node_modules/css2stylus/bin/css2stylus:65:8)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:118:18)
at node.js:952:3

http://fake.ly/dS5a/Image%202015-10-06%20at%204.13.50%20PM.png

when using variables in shell you access variables like this "KMVAR_Path"
also I would add double quotes surrounding the variable

css2stylus “KMVAR_Path”

Tried “KMVAR_Path”, “$KMVAR_Path”, ‘KMVAR_Path’, [‘KMVAR_Path’]… All of them is giving out an error… Nothing else I should add or do?

That's how I use it.

Hey @FakePilot,

When you’re troubleshooting this sort of thing always make certain the shell script is right.

So, use a Display Text (in window) action to show it – and run that from the terminal to make sure it works.

Run this from an Execute a Shell Script action:

export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
which css2stylus

If you don’t get any output then something is wrong.

Make things simple.

You didn’t understand the variable syntax to use in an Execute a Shell Script action.

If you had displayed it from a Display Text action (in a window) you’d have seen how if failed.

You get the idea.

-Chris

Thanks for tip, I am using the window, the error I posted is from that window.
When I do your command, I get: /usr/local/bin/css2stylus
Which should be correct. Doing this in the Terminal works:

css2stylus /path/to/style.css

It outputs a style.styl file in my home directory. If I do this in KM:

export PATH=/usr/local/bin:$PATH;
css2stylus --v

Then it returns 0.3.0. The version. Which means that it works. So must be $KMVAR_Path. Tried checking what it outputs:

/var/folders/9p/v1b392hs56gg9b9grsg326v40000gx/T/Keyboard-Maestro-Script-6B654087-CDDF-4F64-B68F-972EED09ED49: line 1: /Users/XXX/style.css: Permission denied

So something is wrong with the $KMVAR_Path. What?

Who can say? Post the macro, so we can look at it.

Select your CSS file in the Finder, and run this script from Script Editor.app:

------------------------------------------------------------
tell application "Finder"
  set fSel to selection as alias list
  if fSel ≠ {} then
    set f to first item of fSel
  else
    beep
    return
  end if
end tell

set f to POSIX path of f

set shCMD to text 2 thru -1 of "
export PATH=/usr/local/bin:$PATH;
css2stylus " & quoted form of f

do shell script shCMD
------------------------------------------------------------

If it works then we can definitely get the Keyboard Maestro version working.

If it does NOT work then it's possible there are environment issues to contend with.

-Chris

Thanks. Here is the Macro:
http://fake.ly/dUgh/download/convert%20css%202%20stylus.kmmacros

Here is the error that your code from Script Editor produced:

/usr/local/lib/node_modules/css2stylus/bin/css2stylus:59
  var output = converter.processCss(options).getStylus();
                                             ^ 
TypeError: converter.processCss(...).getStylus is not a function
    at ReadStream.<anonymous> (/usr/local/lib/node_modules/css2stylus/bin/css2stylus:59:46)
    at emitNone (events.js:72:20)
    at ReadStream.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:893:12)
    at doNTCallback2 (node.js:430:9)
    at process._tickCallback (node.js:344:17)

You can drag macros and images into the KM Forum editor to post them; it will accept any web-valid image type, KM file type, or zip file.

You can also use the share button in the KM Editor Toolbar. Click it, select 'Keyboard Maestro Forum', Click the {Both} button. Let the new topic appear in your browser. Copy the result. Cancel the new topic, and paste the data into the topic where you want it.

Run this from the Terminal and post the result (it'll be in the clipboard):

echo $PATH | pbcopy

You only had "$KMVAR_fileName" in your Execute a Shell Script action, so you were literally telling it to execute the file in the given path. No wonder it was having a hissy fit.

As for the other problem – I want to see what the path looks like before speculating further.

-Chris

1 Like

This is what the Execute a Shell Script action should look like:

I never use "Path" as a variable name. It's too easy to confuse it as terminology from something.

I prefer to use something like pathStr, so it's very, very obvious what the variable is.

Failure to quote the variable $KMVAR_VariableName will give nasty surprises if the path has any spaces in it.

In general it's good practice to always quote variables in the shell:

cat "$KMVAR_pathStr"

-Chris

Thank you so much.

I guess the path is "screwed up" by a Ruby installment? Can I change that somehow or shouldn't the "export PATH=/usr/local/bin:$PATH;" help fix that?

/Users/Fake/.rvm/gems/ruby-2.0.0-p195/bin:/Users/Fake/.rvm/gems/ruby-2.0.0-p195@global/bin:/Users/Fake/.rvm/rubies/ruby-2.0.0-p195/bin:/Users/Fake/.rvm/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin

Ah, the "$KMVAR_fileName" was only there for testing. Had the rest in a disabled "Display text" field.

You've apparently missed the fact that KM establishes a brand new Terminal environment when it runs, and it does not use any of the preferences established for Terminal.app – no .profile – no nothing.

This is also true of AppleScript.

There are ways to load your environment, but I don't remember off the top of my head.

If you run:

echo $PATH

From a Keyboard Maestro Execute a Shell Script action – to a text window you'll see it does not compare to what you got from the Terminal app.

Breaking up your path I find there is one duplicate:

/bin:
/opt/X11/bin:
/sbin:
/Users/Fake/.rvm/bin:
/Users/Fake/.rvm/gems/ruby-2.0.0-p195@global/bin:
/Users/Fake/.rvm/gems/ruby-2.0.0-p195/bin:
/Users/Fake/.rvm/rubies/ruby-2.0.0-p195/bin:
/usr/bin:
/usr/local/bin:
/usr/local/bin: <-- DUPLICATE •••••
/usr/local/git/bin
/usr/sbin:

I would try running the above path command in KM and then use it to mimic the path you got from the Terminal.

Remember to subtract out what you get from echo $PATH and then add in the above items WITHOUT any duplicates.

You should end up with something close to this:

export PATH=/usr/local/bin:/usr/local/git/bin/opt/X11/bin:/Users/Fake/.rvm/bin:/Users/Fake/.rvm/gems/ruby-2.0.0-p195@global/bin:/Users/Fake/.rvm/gems/ruby-2.0.0-p195/bin:/Users/Fake/.rvm/rubies/ruby-2.0.0-p195/bin:$PATH;

Remember that the $PATH on the end will add the existing path to the pieces you are adding.

Once you've created this full path for KM then you can try your macro again.

-Chris