Copy link to clipboard
Copied
Hello, very new to scripting. I know that I can assign a color to selected objects:
app.documents.add(DocumentColorSpace.RGB)
// get the active document
var doc = app.activeDocument;
// verify there is a document and at least one shape,
// enumerate through each pathItem and assign a given color to fill
if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0) {
var newColor = new RGBColor;
newColor.red = 255;
newColor.green = 0;
newColor.blue = 0;
for (var a=0; a<app.selection.length; a++) {
try {
var currentSelection = app.selection;
// But how would I know whether Stroke or Fill is active here?
currentSelection.fillColor = newColor;
}
catch (e){alert(e)
}
}
}
With AutoHotKey, I can use ControlGetText on the input element of the Color Panel ("Edit 15") to directly interact and easily apply colors by submitting a new value within it, which accurately sends it as a stroke or fill depending on which is currently toggled. Is there any way to replicate this and have access to the current/active value within this panel?
You probably need the Application.isFillActive() method.
Copy link to clipboard
Copied
If I understood correctly, then try:
if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0) {
var newColor = new RGBColor;
newColor.red = 255;
newColor.green = 0;
newColor.blue = 0;
// set fillColor for selection
activeDocument.defaultFillColor = newColor;
// set StrokeColor for selection
activeDocument.defaultStrokeColor = newColor;
}
Copy link to clipboard
Copied
Hi,
Not sure you have to be concerned by either the stroke or fill is active inside the tool/color panels prior to apply either a stroke or a fill.
If you want to ensure a specific fill/stroke color isn't used, then grab it and see its value…
if ( someItem.fillColor.[some attributes] != …)
Copy link to clipboard
Copied
Thanks Alexander! That solves half my problem.
Also Loic, I do given how I plan to use it. This isn't for a standalone script but for a panel that will assign a new color dynamically. I should've explained more clearly: I don't want to assign both a stroke and a fill or to check if a specific color is/isn't used, I only want to assign a color as a fill if fill is active and only stroke if stroke is active -- but never both at the same time, never assign the color to a fill if stroke is active and vice versa. Since this is for a panel it's passive and meant to be used organically multiple times throughout a natural workflow, just like the current Colors panel. I've yet to see any examples of this or any kind of conditional that determines it in any of my searching, but it does concern me.
Copy link to clipboard
Copied
You probably need the Application.isFillActive() method.
Copy link to clipboard
Copied
That sounds like exactly what I need, thank you. I can't test while at work and on mobile just yet -- I'm curious though, where is this, documentation-wise? It's not in the Illustrator Scripting Reference under methods for application.
Copy link to clipboard
Copied
In the ExtendScript ToolKit, the application used to debug extendscripts, you can invoke the Object Model Viewer. This is accessible via the menu in the ESTK and pulls u a search-able reference window where you can see all the different methods and classes - many of which are not mentioned in the PDF documentation.
However, this is the case for Illustrator - other applications vary in their documentation content when it comes to the PDF and the OMV representation.. for example, the Photoshop description xml file has to be downloaded and added to some folder by the user because unlike Illustrator, they seemed to have forgotten to include this with Photoshop.
Copy link to clipboard
Copied
That seems really odd, thanks again -- I didn't realize the PDF documentation wasn't fully inclusive but you've saved me hours of guesswork so I can't stress appreciation enough. Admittedly I dropped ESTK (Is there any way to update it for auto-completion, bracket highlighting, or other standard text-editor features? It seems really antiquated in comparison to things like Sublime, Atom and Brackets...?) to use Atom in its place, but see now I should be using them together if nothing else.
Copy link to clipboard
Copied
Silly-V,
This thread has already been a game changer. I did not know there was more documentation in ESTK than what the documents hold.
What I did not see, is there anyway to change the property? so that I can flip between stroke and fill active states from a script.
By the way, dynamic actions thread was a big help. Thank you.
Copy link to clipboard
Copied
Aha! Use the isFillActive property along with a 'set color' recorded action. This action not only sets the fill like JSX's 'setDefaultFill' - or whatever it was - but it also has the effect of switching the stroke/fill widget to have the fill up-front. To switch, you don't even need to use isFillActive if you simply want to set the fill regardless of where it was before. You don't even have to create a temporary piece of art to fill because this action appears to work even with no selection.
Copy link to clipboard
Copied
Hi,
Not sure you have to be concerned by either the stroke or fill is active inside the tool/color panels prior to apply either a stroke or a fill.
If you want to ensure a specific fill/stroke color isn't used, then grab it and see its value…
if ( someItem.fillColor.[some attributes] != …)
Copy link to clipboard
Copied
Works, thanks for the pointers guys.
var isFill = app.isFillActive();
if (isFill) {
alert("Fill is active");
} else {
alert("Stroke is active");
}
Admittedly I think it's strange that, despite earnestly trying to read a lot of the documentation on SDK, CEP and JavaScript (because it's not too clear what I should be aiming for? There isn't an outline saying "If you want this, then pursue this route, etc., it's all bundled together) extensions for the last two months in preparation of all this, I don't remember a single mention of the Help > Object Model Viewer from Extendscript that SillyV pointed out. Am I wrong in saying that? Also that even inside this OMV, there aren't any samples and the description snippets are given in pseudo-code abstractions like "Application.isFillActive" instead of "app.isFillActive" directly despite the fact this isn't under a cross-platform specification and is explicitly a class for Illustrator 22 Type So why isn't it "app.isFillActive()", to avoid confusion, or a simple example of it in use? Maybe it's just me, but some of the documentation seems very obtuse -- like how in the JavaScript Scripting Reference, it explicitly says something to the effect of "There aren't many samples and what samples there are won't necessarily be the best way to do them in JS so if you want to find out something you'll have to extrapolate the info needed over the entirety of samples given (for all hundreds of pages, of which what seems like 90% I would never need to touch because I'm not in the printing business). It's the reason I've felt the need to edge towards this over the course of months rather than just jump into it and learn from direct engagement, even though I've been using Illustrator, After Effects and Photoshop for years and am really familiar with the programs.
Copy link to clipboard
Copied
Hmm, what can we say, the specialized field of this kind of tools development is affected by the version history of the applications - it's indeed years of experience with both the regular usage of the software as well as automating it which enable one to solve their extensibility challenges. The key will be to train oneself to retrieve the answers for the correct questions about the work, which is the greatness of this very forum. By finding yourself here, you can tap into the 'true' documentation of Illustrator scripting.
It is apparent from your initial approach that you model your process by directly translating the user actions into your logic, since you were talking about UI scripting. The UI scripting will inevitably fail due to possible updates in the UI of any program.
Therefore it becomes necessary to create practice scripts to learn the scripting commands for the application in order to know what can be automated by their provided abilities. Some features that users can easily change in the UI are actually no manipulable by scripting and we sometimes do have to rely on bits of UI scripting as a last resort.
So unfortunately what you've found yourself in is a barrier of learning curve which slows your stride. However, with the help of this forum and snippets posted here and there, you can achieve this mind-set and leverage the scripting api to be a vital component within your automation capabilities.
Also compare this to the Photoshop scripting world where the OMV for PS has to be hunted down and manually added to the OMV folder, and how many of the Photoshop commands aren't available through the scripting api but rather a javascript programmatically-generated action-descriptor code which is recorded by a special script-listener plugin. Quite different than Illustrator and takes much getting used-to, but after initial grumbles and complaints, it's all about getting the work done, and after a while you're over it.