Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

crop to selection

New Here ,
Jul 21, 2008 Jul 21, 2008
Hi, <br /><br />I have a script that I'm working on to open all the images in a folder, resize, set a fixed selection and then crop to the selection. My problem is that I can't seem to crop to the selection. The crop method is expecting an array but apparently not the array of the documents selection. <br /><br />function setTheSelection(){<br /> var openedImages = app.documents;<br /><br /> for(var i=0; i<openedImages.length; i++){<br /> var curImg = openedImages;<br /> var curWidth = curImg.width.as("px");<br /> var curHeight = curImg.height.as("px");<br /> var topCord = 0;<br /> var leftCord = 0;<br /> <br /> app.activeDocument = curImg;<br /> <br /> if (curWidth == 400 && curHeight > 275){<br /> var extra = Math.floor(curHeight-275); <br /> if (extra > 120){<br /> topCord = 20; // This is really only good for a few images. Commented out in the current production script<br /> } else{ <br /> topCord = Math.floor(extra/3);<br /> } <br /> }<br /> if (curHeight == 275 && curWidth > 400){<br /> var extra = Math.floor(curWidth-400);<br /> leftCord = Math.floor(extra/2);<br /> }<br /> // Select a square area at the top of the new document <br /> var selRegion = Array(Array(leftCord, topCord), <br /> Array(leftCord+400, topCord), <br /> Array(leftCord+400, topCord+275), <br /> Array(leftCord, topCord+275), <br /> Array(leftCord, 0))<br /> curImg.selection.select(selRegion, SelectionType.REPLACE, 0)<br /> curImg.crop(selRegion); /*!!! Here's the problem, but I don't understand why !!! */<br /> } <br />}//setTheSelection<br /><br />Am I misunderstanding the crop method?
TOPICS
Actions and scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Explorer ,
Jul 21, 2008 Jul 21, 2008
Johnny_Hernandez@adobeforums.com wrote:

> Am I misunderstanding the crop method?

Don't do it like that. After you have your selection made, do this:

executeAction( app.charIDToTypeID('Crop'), new ActionDescriptor(),
DialogModes.NO );



ScriptingListener is very handy for stuff like this.

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 23, 2008 Jul 23, 2008
Thanks. This worked for me. Thanks also for the tip on ScriptListener. I wasn't aware of that plug-in.

-j
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 24, 2008 Jul 24, 2008
Just remember to remove the ScriptListener plugin when you're done recording, and only install it when you need to record something. It will slow down your application tremendously if you leave it in all the time.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 24, 2008 Jul 24, 2008
Mark_Walsh@adobeforums.com wrote:
> Just remember to remove the ScriptListener plugin when you're done recording, and only install it when you need to record something. It will slow down your application tremendously if you leave it in all the time.

I have mine permanently installed. Then again, I'm in a perpetual software
development mode, too.

Has anybody done any real analysis as to the performance cost? I can't see that
the impact would be anymore than a few percent, if that. I suspect that a RAID0
10K/rpm pair of drives or even just tweaking PS performance preferences would
have a far greater impact.

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 25, 2008 Jul 25, 2008
I have always pulled it out when I wasn't using it. I remember feeling a difference on my computer as it was writing each step to the log file. That's interesting to know that you leave it in all the time. I thought that the manual mentions only installing it when it's needed. I guess it also depends on how extensively you are using Photoshop.

I would also be interested in knowing the actual performance hit if anyone has done any real analysis. It is always quite possible that it 'felt' slower because I knew that it was installed. (although I do believe there was one time that I had forgotten to remove it and noticed a while later when the program felt slow)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 12, 2008 Sep 12, 2008
Interesting...would this script crop to a selection that is not square for instance a magic wand or lasso selection around a shape such as a bottle?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 12, 2008 Sep 12, 2008
> Interesting...would this script crop to a selection that is not square for instance a magic wand or lasso selection around a shape such as a bottle?

It will crop to the selection regardless of how the selection was created.

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 13, 2008 Sep 13, 2008
I leave scriptlistner installed all the time as well. I did change the VB log to read only. Photoshop appends to the end of the log and I would think it would only slow down Photoshop if the system's disk I/O is slow.

At least I don't notice any difference.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2008 Sep 15, 2008
Am I misunderstanding the crop method? Well I don't understand your JavaScript code but in AppleScript I don't think you can go from the selection object to the crop command like you can in the UI. If you have a bounds array you just put that into the crop command in AS it would look like this with no need to create a selection.

tell application "Adobe Photoshop CS2"
set ruler units of settings to pixel units
activate
set Doc_Ref to the current document
tell Doc_Ref
crop bounds {100, 100, 500, 500} angle 20
end tell
end tell

ScriptListener is always loaded for me and I can't say I see any performance differences. Only the slight delay as this plug-in loads. My mac boots and launches apps on schedule so I never see this anyhow.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 15, 2008 Sep 15, 2008
Mark_Larsen@adobeforums.com wrote:
>Well I don't understand your JavaScript code but in AppleScript I don't think you can go from the selection object to the crop command like you can in the UI.

The line of code I posted earlier in this thread is the same as selecting the
Image->Crop menu item when you have a selection active.


> If you have a bounds array you just put that into the crop command in AS it would look like this with no need to create a selection.

The selection may have already been created via some other mechanism (loading a
mask-based selection) and you want to crop based on that. And prior to CS3,
getting the bounds of a selection wasn't very easy.

JS has a similar api (Document.crop) for doing a bounds-based crop.

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2008 Sep 15, 2008
X, I know the solution you gave in your first post is the same as calling the UI menu item. Your tool box has taught me a few tricks I could never have done. I used to use system events to do some of this stuff but no longer do with Photoshop. It just looked like the array was being calculated using scripted math? I understand your solution would work if a selection was activated by some other load channel method. I just recalled my failings when just calling a crop command with an active selection which differed from the UI. Selection bounds goofed me for a while. Document.crop would be the equivalent to what I was trying to say.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 15, 2008 Sep 15, 2008
Mark_Larsen@adobeforums.com wrote:
>It just looked like the array was being calculated using scripted math?

The underlying problem with the code in the original problem was that a
selection region was being passed to 'crop' instead of the actual bounds of the
area to be cropped. So the selection is not really needed. A call to
Document.crop with the correct bounds array would have been sufficient.

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 18, 2008 Sep 18, 2008
LATEST
Would it be too much to ask to have you post the entire script as it should look?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines