79533858

Date: 2025-03-25 13:48:39
Score: 0.5
Natty:
Report link

A GUI tool for macOS showing the stack after a PostScript code, with a manually entered input stack

On macOS, you can build a graphic tool which show the stack after running a code.

(Disclaimer: I was helped with an AI for a part of the AppleScript code, specially the line set shellCommand to ..., as I needed a stack viewer for learn the PostScript code).

  1. Open the Shortcut app and click on "+" to create a new shortcut. By default, you obtain a window with no actions, and a default title "Title". Click on the defautl title and rename it "PostScript Stack viewer". You can also change the default icon and color. For example, search for a spanner.

Change the icon

  1. In the right side of the windows, search for the action "Ask for Input", add it (double click or dragging).

Add "Ask for Input" action

  1. Click on "Prompt" and change it for Input stack (ex. "3 4").

"Input stack" prompt

  1. Add a second action "Ask for Input" under the first, and change "Prompt" with "Input the PostScript code (ex. "add")". Click on Show more and be sure that Allow multiples lines is checked.

"Input the PostScript code" prompt

  1. Search the "Text" action and add it under the two previous actions, then right-click on the field and choose "Insert Variable > Select Variable"

Insert a variable

Then click on the first "Ask for input" (cercled in red here):

Select the first "Ask for input"

This will add the input stack in the Text object.

The stack input content is added in the Text object

  1. Add a space, then right-click on the field after the space and choose again "Insert Variable > Select Variable". Then select the second "Ask for input" (which contain the PostScript code). This will give this Text object:

The PostScript code content is added in the Text object

Notice the space between the two graphical variables, it's very important. You can know to what "Ask for Input" each object is by click on it (not right-click), then choose "Reveal". You can also rename it with this method).

Reveal the variable

  1. Search for the "Run AppleScript" action and drag it under the previous actions.

Search for "Run AppleScript"

It automatically takes the previous Text object as input.

AppleScript action added

Note that adding the "Run AppleScript" can replace the icon choose for the Shortcut by the AppleScript icon. You can change the icon again (click on it, see point 1).

  1. Paste this AppleScript code in the field (the variable completeCommand receive the value in the Text object):
on run {completeCommand}
    
    -- Prepare the PostScript command for Ghostscript
    set fullPSCode to (completeCommand as text) & " pstack"
    
    -- Run Ghostscript via do shell script with the full path
    try
        set shellCommand to "echo " & quoted form of fullPSCode & " | /usr/local/bin/gs -q -sDEVICE=nullpage - 2>&1"
        set output to do shell script shellCommand
        
        -- Define the delimiters to include \r (carriage return)
        set AppleScript's text item delimiters to {return, linefeed, ASCII character 13}
        set resultLines to text items of output
        
        -- Reverse the order to go from the base to the top
        set reversedLines to reverse of resultLines
        
        -- Build the final result
        set cleanResult to ""
        repeat with thisLine in reversedLines
            if length of thisLine > 0 then
                set cleanResult to cleanResult & thisLine & " "
            end if
        end repeat
        
        -- Clean the final space and reset the delimiters
        if length of cleanResult > 0 then
            set cleanResult to text 1 thru -2 of cleanResult
        end if
        set AppleScript's text item delimiters to ""
        
        display dialog "Output Stack: " & cleanResult buttons {"OK"} default button "OK"
    on error errMsg
        display dialog "Error: " & errMsg buttons {"OK"} default button "OK" with icon stop
    end try
end run

You can click on the Hammer icon of the AppleScript action to "compile" the code. If the code is syntactically colored, then there is no error.

Compiled AppleScript code

In this code /usr/local/bin/gs this the path to the gs command. You need to use the absolute path.

  1. On the right side, click on the information icon ⓘ and click on "Pin in Menu Bar".

Pin in Menu Bar

  1. Quit the Shortcut app, click on the Shortcut menu in the menu bar, and choose the "PostScript Stack viewer" shortcut.

Run the "PostScript Stack viewer" shortcut

Enter the input stack:

Enter the input stack

Click done, enter the PostScript code, example add, click done, and you have the output stack:

Output Stack

Another example:

Output stack:

Output for "1 2 3 4 5 6" and "pop dup mul 3 1 roll"

Reasons:
  • Blacklisted phrase (0.5): I need
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: quark67