How can I set the focus back onto my program?

Brothernod
Brothernod used Ask the Experts™
on
I wrote a program that draws something in a picture box and then copies it to the clipboard and then pastes it into photoshop using photoshop scripting.

WELL i ran into a weird program.  If I have already set the programs focus on Photoshop, photoshop won't recognize that I placed something in the windows clipboard, it's almost like it has it's own clipboard...

If I copy it to the clipboard before I start working with photoshop then it works fine.

So is there anyway that I can like set focus on my program??
Any ideas?
I know that no one here is familiar with photoshop scripting, but i'm pretty sure that I can handle this with some vb manipulation and not have to try and get the attention of the few people that frequent the adobe boards.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:

Can we see the code so we can try it (or some small example code)?  Is your VB code something like this:


<Draw stuff on the picture box here, using .Line etc...)

Clipboard.SetData Picture1.Image, vbCFBitmap



Are you talking about Photoshop Action scripts?  If so, do you just have a script that has Paste in it?

Commented:

Do you have the AutoRedraw property for the Picture Box set to True?  It defaults to False, but it must be set to True if you are drawing on the Picture Box.

Author

Commented:
Yeah. that is the code I have and the autodraw is true.
If you have photoshop 7, i'll through together some sample code to show you what i'm talkin about.

Yeah I have an action to paste the image from the clipboard, but as I said it acts like it doesn't exist.
If you don't have photoshop 7, no point in sample code really, but again, if u have it i'll through something together.
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
http://web.njit.edu/~jms7/other/temp/SampleCode.zip

I have uploaded a program I threw together to demonstrate the probelem.  I have a lot of comments in it to explain what's going on so I don't want to make this thread all messy posting the whole thing, unless some people here aren't comfortable downloading a zip file (which i suppose i can understand).


In order to run this program you need Photoshop 7 and you need to install the scripting plug in from here :  http://www.adobe.com/support/downloads/detail.jsp?ftpID=1536

If anyone wants I can just post the code in here i suppose, whatever will get more help for my strange problem.

Thanks.
(and here, some more points for incentive)

Commented:
Have you tried


SetForegroundWindow <app's>.hwnd
SendKeys "^v", True

Author

Commented:
What does that code translate to in english??

It's weird because the image does get copied to the clipboard, photoshop just chooses not to recognize it.

Commented:

Well, it seems like some timing issue or something.  I mean, if you add another button and put the paste action into that other button it will work (strange as it is).  I just got to working on it, but maybe that extra bit of information would be helpful too you.

Commented:

I think rehand is trying to tell you to send a paste keystroke to the application.  However, that won't work either because the paste menu option is not available.  I see what you mean by it just not seeing the change to the clipboard.  Really a strange problem.  Even when you add the extra button like I said above it doesn't always work.  It works most of the time, but now and then it gets stuck in the mode that gives that error again.  Really strange....let me try some more tinkering.

Author

Commented:
Thanks Enlade.  Weird problem isn't it??

Do you think if you threw in like a 2 second delay or something between the copy and paste it would work??

Commented:

I tried a delay and I also tried a timer (just to see what would happen).  They didn't work.  So I think it is something that is being called on the return from the press of the first button (but refresh and doevents don't do anything).  I'm going to go eat and maybe think this through later tonight.  Take care.

Author

Commented:
I'm not at home so I can't test these ideas but here's 2.

1) Put in a do while until the clipboard value changes and have it loop through the process
2) Put mutiple Clipboard.SetData picture1 lines in a row.

What do you think??

Commented:

There isn't any way to check the clipboard value.....though maybe if we trap the error until it doesn't occur anymore then it might work.  I'll try it now.

The second suggestion I already tried.  It doesn't work.  Same problem.

Commented:

Ok, this is strange....but very promising.  I put in an on error resume next and a check of the Err object and it seemed to fix the problem.  However, the error is never triggered.  If you put a break on the Goto it will never break there.  Now, that is very weird.  Anyway, here is the code.


-----------------------------------------------------------------------------

Public docRef As Photoshop.Document
Public appRef As Photoshop.Application

Private Sub Command2_Click()
    'Open Photoshop
    Set appRef = New Photoshop.Application
    Set docRef = appRef.Documents.Add(4, 1, 72, , psOpenRGB)
   
    appRef.Preferences.RulerUnits = psInches
       
   
    'Draw picture
    Call DrawPic(1)

    'Copy picture to clipboard
    Clipboard.Clear
    Clipboard.SetData Picture1.Image    
   
Junk:
    On Error Resume Next
    docRef.Paste
    If Err.Number <> 0 Then
      GoTo Junk
    End If

End Sub


This seems to work for me for some odd reason.  You might think that it is working because the program loops until it is successful with the paste; however, the GoTo statement is never called.  That means that with this code inserted into your program the first call to docRef.Paste is successful.  That is very strange if you ask me.  Give it a try.

Commented:

I took another peek at this code today and although it gets rid of the error it still doesn't do what you want it to do.  It does some other strange stuff.  I have one other idea that I want to try.

Author

Commented:
Here's something someone else noticed about this.

If do Command1, then close the document (not photoshop) then do Command2, it works.

and from then on in that instance of photoshop command2 will work as long as you close everything before you run it.

Oh, and I'm increasing the points to 500, just to hopefully keep ya enthused just that little bit longer :)

Commented:

You know....its almost like they don't want to allow you to pass images into Photoshop.  I mean, I guess that would allow people to use photoshop to do the graphic processing for other applications.  I don't know why that would be a big deal, but everything about this add in seems to foil any oppertunity to process a bitmap that is generated by another application.

Think about it.  Why on earth are you even passing the bitmap through the clipboard?  Why wouldn't Adobe just add a method that allowed you to pass a bitmap directly to a Layer within your document?  They allow you to create a text layer and pass the text view the TextItem property.  So, why not have an ImageItem property as well?  All I can think of is that it gives other application developers the ability to use Photoshop to process images using Photoshop, but displaying and inputing then within their own application.  Maybe I'm just paranoid, but that is the only reason that I can think of that they don't have a build in ImageItem property and/or a method to pass images via the clipboard from some external application.

I hope whoever is developing this horrible, horrible, Photoshop Scripting add in will hear me and fix it so that the silly thing is the least bit useful.  I noticed that you posted a message in the Adobe forums on this subject and maybe you can inform them that they can make their scripting add in much better if they had an ImageItem property in the Layers just like they have a TextItem property.  That would really make this silly add in useful.

Anyway, I have a work around that might work for you.  Mind you, even the work around is a pain in the butt since again they seem to be trying to prevent us from passing bitmaps between applications.  Still, they could not prevent it completely.  Maybe this code will work for you:


------------------------------------------------------------------------------------

Public docRef As Photoshop.Document
Public appRef As Photoshop.Application

Private Sub Command1_Click()
  Set appRef = New Photoshop.Application
  appRef.Preferences.RulerUnits = psInches
                     
  'Draw picture
  Call DrawPic(0)

  SavePicture Picture1.Image, "C:\Junk.BMP"
  Set docRef = appRef.Open("C:\Junk.BMP")
  docRef.Selection.SelectAll
  docRef.Selection.Copy
  docRef.Close
 
  Set docRef = appRef.Documents.Add(4, 1, 72, , psOpenRGB)
  docRef.Paste
   
End Sub

Private Sub Command2_Click()
  Set appRef = New Photoshop.Application
  appRef.Preferences.RulerUnits = psInches
                     
  'Draw picture
  Call DrawPic(1)

  SavePicture Picture1.Image, "C:\Junk.BMP"
  Set docRef = appRef.Open("C:\Junk.BMP")
  docRef.Selection.SelectAll
  docRef.Selection.Copy
  docRef.Close
 
  Set docRef = appRef.Documents.Add(4, 1, 72, , psOpenRGB)
  docRef.Paste
 
End Sub


You see it saves the image to disk as a bitmap and then tells Photoshop to load it.  Note that even while trying this I got errors that would not allow me to do it in other ways.  For instance, why can't I load an image into a pre-existing document (layer)?  Why can't I release the association between the open document and the file?  You see, if I simply load the bitmap into a document it will not allow me to overwrite that file again for the next bitmap I want to pass.  So, instead I have to load the bitmap and then copy it over to another newly created empty document.  Then I can close the Bitmap file and that way I can overwrite it again for the next bitmap that I want to pass to Photoshop.  It just is a very long about way of passing bitmaps to Photoshop.  It could have, and really should have, been much more easy.

Your first question shouldn't have been how you could pass the bitmap using the clipboard, but rather how you can pass the bitmap directly to a document layer (without every dealing with the clipboard).  It should be easy enough for them to create a method that allowed you to pass a bitmap directly, just like they do with the TextItem property.  It makes me kind of mad because it is just poor design and it made us pull our hair out over it for a number of days.  Anyway, I think that the above code is the only way that you are going to be able to pass your bitmaps to Photoshop.  Tell me what you think?
Commented:
Holy cow, I couldn’t even read what I wrote.  Let me try this again.  Please ignore my message above and read this one instead.

----------------------------------------------------

You know....its almost like they don't want to allow you to pass images into Photoshop.  I mean, I guess that would allow people to use photoshop to do the graphic processing for other applications without giving enough credit to photoshop.  I don't know why that would be a big deal, but everything about this add in seems to foil any opportunity to process a bitmap that is generated by another application.

Think about it.  Why on earth are you even passing the bitmap through the clipboard?  Why wouldn't Adobe just add a method that allowed you to pass the bitmap directly to a Layer within your document?  They allow you to create a text layer and to pass text to that layer using the TextItem property.  So, why don’t they have an ImageItem property as well?  All I can think of is that it gives other application developers the ability to use Photoshop to process images without giving enough credit to photoshop.  That is most of the input and output are done independent of photoshop and then people can pretty much ignore that photoshop is even loading.  After all, you shouldn’t even have to see the photoshop application open.  It should open in the background (like it does with the Word Object when you use word to do some text processing for your application).  So the optimal solution for developers is to hide Photoshop completely even though they are using it to actually do their image processing.  That is the way it should be.  I mean, you still gotta buy Photoshop and install it before any of this works anyway.  So why should they even care.  Maybe I'm just paranoid, but it just seems like they don’t like the idea of developers using the Photoshop image processing without Photoshop making its presence known.

I hope whoever is developing this horrible, horrible, Photoshop Scripting add in will hear me and fix it so that the silly thing is the least bit useful.  I noticed that you posted a message in the Adobe forums on this subject and maybe you can inform them that they can make their scripting add in much better if they had an ImageItem property in the Layers just like they have a TextItem property, and also if you can open the application in background mode so that the user doesn’t have to see Photoshop at all to use your program.  That would really make this silly add in quite useful.

Anyway, I have a work around that might work for you.  Mind you, even the work around is a pain in the butt since again they seem to be trying to prevent us from passing bitmaps between applications.  Still, they could not prevent it completely.  Maybe this code will work for you:


------------------------------------------------------------------------------------

Public docRef As Photoshop.Document
Public appRef As Photoshop.Application

Private Sub Command1_Click()
 Set appRef = New Photoshop.Application
 appRef.Preferences.RulerUnits = psInches
                     
 'Draw picture
 Call DrawPic(0)

 SavePicture Picture1.Image, "C:\Junk.BMP"
 Set docRef = appRef.Open("C:\Junk.BMP")
 docRef.Selection.SelectAll
 docRef.Selection.Copy
 docRef.Close
 
 Set docRef = appRef.Documents.Add(4, 1, 72, , psOpenRGB)
 docRef.Paste
   
End Sub

Private Sub Command2_Click()
 Set appRef = New Photoshop.Application
 appRef.Preferences.RulerUnits = psInches
                     
 'Draw picture
 Call DrawPic(1)

 SavePicture Picture1.Image, "C:\Junk.BMP"
 Set docRef = appRef.Open("C:\Junk.BMP")
 docRef.Selection.SelectAll
 docRef.Selection.Copy
 docRef.Close
 
 Set docRef = appRef.Documents.Add(4, 1, 72, , psOpenRGB)
 docRef.Paste
 
End Sub


You see it saves the image to disk as a bitmap and then tells Photoshop to load it.  Note that even while trying this I got errors that would not allow me to do it in other ways (better ways), and I ran into other restrictions that prevented me from doing it in a more natural way.  For instance, why can't I load an image into a pre-existing document (layer)?  Or, why can't I release the association between the open document and the file (the file is locked when it is loaded into Photoshop—I’d like to unlock it and make the document that I loaded an Untitled document)?  You see, if I simply load the bitmap into a document it will not allow me to overwrite that file again for the next bitmap I want to pass.  So, instead I have to load the bitmap and then copy it over to another newly created empty document.  Then I can close the Bitmap file and that way I can overwrite it again for the next bitmap that I want to pass to Photoshop.  It just is a very long about way of passing bitmaps to Photoshop, but it really should have been very easy (one ImageItem property would have done it).

Anyway, your first question really shouldn't have been how you can pass the bitmap using the clipboard, but rather how you can pass the bitmap directly to a document layer (without every dealing with the clipboard).  It should be easy enough for them to create a method that allowed you to pass a bitmap directly, just like they do with the TextItem property.  It makes me kind of mad because it is just poor design and it made us pull our hair out over it for a number of days.  Anyway, I think that the above code is the only way that you are going to be able to pass your bitmaps to Photoshop.  Tell me what you think?

Author

Commented:
Really sorry about the delay, been busy moving back into school, I just played with it and it works great.  Thank you so much it is appreciated.  Though not the optimal way to do it, it works, and it looks like it won't cause problems with the rest of my program.  Again, thanks.
Really impressed.

Here are your well deserved points.
Da_WeaselSenior Software Analyst

Commented:
Just a side note for this issue:
Lots of applications won't recognize clipboard data added prior to them starting up.  Visual Basic 6 is one of them.
Test this with the following:
Open Notepad.exe and type some text.
Copy it and paste is to various places.
Open VB and try to paste it there. (won't work)
Go back to Notepad.exe and copy the text again.
Now try pasting to VB.  (will work this time)

You may want to try this with the above code.
Start Photoshop first.
then copy the data to the clipboard.
Then do what ever you need to do.

Commented:
Da_Weasal

I have also notice that when you write a VB application that copies information into the clipboard, that it is not immediately available for retrieval. Do you know of any work arounds for this scenario?
Da_WeaselSenior Software Analyst

Commented:
Win32 API might get it there faster....never played much with he Clipboard from VB myself though...

Author

Commented:
Da_Weasel, you have it a little backwords.  The problem was that photoshop recognized what was in the clipboard if I put it there before photoshop started, but once photoshop started it wouldn't recognize it.  Mind you this was only when done with code as normally doing stuff in windows I have no problem pasting stuff into photoshop.

I used the above workaround for quite some time, but recently I went back and just had the program draw straight into photoshop rather then into a picture box... it's actually much more efficient.

Still doesn't explain why the problem existed in the first place.
Da_WeaselSenior Software Analyst

Commented:
Prolly bad coding in Photoshop....

Author

Commented:
The way the rest of their scripting package is half assed it would be no suprise to me.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial