OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-02-2004, 04:31
Seelenquell
Guest
 
Posts: n/a
Default is application running?

Hi all!

i´ve been wondering if there´s a possibility to test if an application is running or not.

example:
because of the weird bitmap-convert-thing cmyk2rgb in coreldraw itself i´ve started creating a macro in coreldraw to export selection to bitmap (TIFF) and to open it in photoshop 7 (per vba), convert it there to rgb, save it and close it, then import it in coreldraw.
but if the application itself (here photoshop) isn´t runnin' i´ll get an error-message. so the question is, how can be tested if photoshop is runnin or not? and if not, how can it be started per vba?

thank you all for your suggestions

Reply With Quote
  #2  
Old 02-02-2004, 08:31
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,941
Blog Entries: 4
Default Re: is application running?

First question: how do you connect to Photoshop? Do you use GetObject method in VBA? If so, I would use CreateObject instead because this is supposed to launch the application if it is not already running (provided that Adobe implemented this correctly).

If this doesn't work, then probably the following code might help:

Code:
Option Explicit

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Const INFINITE = &HFFFF      '  Infinite timeout

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, _
                                ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
                                ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
                                ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long

Sub EnsurePhotoshopRunning()
    Dim hWnd As Long
    Dim nRet As Long
    Dim pi As PROCESS_INFORMATION
    Dim si As STARTUPINFO
    
    hWnd = FindWindow("Photoshop", vbNullString)
    If hWnd = 0 Then
        ' Photoshop is NOT running. Launch it.
        si.cb = Len(si)
        nRet = CreateProcess(vbNullString, "C:\Program Files\Adobe\Photoshop\Photoshop.exe", 0, 0, 0, 0, 0, vbNullString, si, pi)
        If nRet = 0 Then
            MsgBox "Unable to launch Adobe Photoshop", vbCritical
        Else
            WaitForInputIdle pi.dwProcessId, INFINITE
            CloseHandle pi.hProcess
            CloseHandle pi.hThread
        End If
    End If
End Sub
Basically the way it works is quite straightforward. First it looks for Photoshop window (a window with "Photoshop" class which is the one belongs to Adobe Photoshop). If not found, it starts the application by launching it, waits till the application initializes and ready and then exits.

Make sure you have the correct application path in CreateProcess method.
Reply With Quote
  #3  
Old 02-02-2004, 09:17
Seelenquell
Guest
 
Posts: n/a
Default Photoshop

Hi Alex,

first of all thank you for your fast reply.

i referenced the 'adobe photoshop 7 object library' in my vba and it works perfectly..

i thought, there would be a simple way like

if application.photoshop.isrunning then makethethings else photoshop.start

sorry i am a beginner in vba

thanks for your help
Reply With Quote
  #4  
Old 08-02-2004, 18:38
Mark
Guest
 
Posts: n/a
Default Illustrator

Does anybody know the window classname for Illustrator CS?
Reply With Quote
  #5  
Old 08-02-2004, 19:57
Mark
Guest
 
Posts: n/a
Default Illustrator

Nevermind..got it. Illustrator9

Spent so much time looking on the web for it, instead of just finding a getclassname function.
Reply With Quote
  #6  
Old 08-02-2004, 20:23
Mark
Guest
 
Posts: n/a
Default Maybe I'm not out of the water yet

Ok I have a script to first check if a selected program is running, in this case Illustrator...and if not you have the option to launch it. If it is running I was checking for open documents. I have a problem if illustrator is not running, you launch it. Its starting up for sure but when i run the script again it crashes at

Run time error 462
The remote machine does not exist or is unavailable.

Code:
If Illustrator.Documents.Count > 0 Then
Using Illustrators vb stuff obviously, but before anyone yells about this being the wrong forum...the purpose of my script was to go over to illustrator if there was an open doc, select all the items if any, and use Alex's PasteAiFormat (Thanks Alex).

I can press end & immediately run the script again & it gets past that. I thought maybe there was something like Corel's Delay VBA load but i don't see anything. Anyone has any idea i'm all ears.
Reply With Quote
  #7  
Old 09-02-2004, 12:05
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,941
Blog Entries: 4
Default Re: Maybe I'm not out of the water yet

Mark,

It looks like you are trying to access the different instance of illustrator. Did you call "Quit" in previous session to shut down the program without releasing the reference to it and then using the old reference to access the Illustrator's functionality?

Could you show the complete code of your program to see how you manage automation sessions?

P.S. I tried to find a download of Illustrator CS COM plugin but can't. Is it now part of the application or still a separate plugin?
Reply With Quote
  #8  
Old 09-02-2004, 22:20
Mark
Guest
 
Posts: n/a
Default Illustrator

Ok...I've narrowed down the problem a bit. I have a form with two buttons, GrabImage & Launch, a listbox...containing "Adobe Illustrator" & "Adobe Photoshop" in it. Upon selecting one, I run the code

Code:
Sub AppList_Click()
    GrabImage.Enabled = False
        AppImage.Picture = LoadPicture("G:\Script Files\Icons\Applications\" + AppList.Value + ".bmp")
        Dim hwnd As Long
        hwnd = IsApplicationRunning(AppList.Value)
        If hwnd <> 0 Then
            Launch.Enabled = False
            If AppList.Value = "Adobe Illustrator" Then
                If Illustrator.Documents.Count > 0 Then
                    GrabImage.Enabled = True
                End If
            End If
            If AppList.Value = "Adobe Photoshop" Then
                If Photoshop.Documents.Count > 0 Then
                    GrabImage.Enabled = True
                End If
            End If
        Else
            Launch.Enabled = True
        End If
End Sub
Let's say neither App was running. I click illustrator, the launch button enables. I click launch...

Code:
Private Sub Launch_Click()
    Call LaunchApplication(AppList.Value)
    Unload Me
End Sub
The form is unloaded and Illustrator launches. If i run the form again, click illustrator, no errors. But if I click photoshop first, launch enables. Then clicking illustrator, launch disables, it comes back as running, but then it crashes on

Code:
If Illustrator.Documents.Count > 0 Then
Pressing end & running the form again...repeat the process & it won't crash. I thought maybe hwnd's value was getting messed up with photoshop, but emptying out the value did nothing to help.
Reply With Quote
  #9  
Old 09-02-2004, 22:39
Mark
Guest
 
Posts: n/a
Default Alex

A new form, haven't really got the hang of Adobe's scripting but....just so you can see what i'm doing. The selection I have on illustrator needs work. Haven't figured out a way to just select all. There's a pageitems but can't seem to figure out how to use it. If it even means what I think it means.

Code:
Private Sub GrabImage_Click()
    If Application.Documents.Count = 0 Then
        MsgBox "Nothing to paste to.  Open a document & try again."
        Exit Sub
    End If
    
    Dim hwnd As Long, UndoLevel As Integer
    UndoLevel = 0
    Clipboard.Clear
    hwnd = IsApplicationRunning(AppList.Value)
    
    If AppList.Value = "Adobe Photoshop" Then
        If Photoshop.Documents.Count > 0 Then
            If Photoshop.ActiveDocument.Layers.Count > 1 Then
                Photoshop.ActiveDocument.Flatten
                UndoLevel = UndoLevel + 1
            End If
            Photoshop.ActiveDocument.Selection.SelectAll
            Photoshop.ActiveDocument.Selection.Copy
            Photoshop.ActiveDocument.ActiveHistoryState = Photoshop.ActiveDocument.HistoryStates(Photoshop.ActiveDocument.HistoryStates.Count - (UndoLevel + 1))
            ActivePage.Layers("Design").Activate
            ActivePage.Layers("Design").Visible = True
            ActivePage.Layers("Design").Editable = True
            ActivePage.Layers("Design").Printable = True
            ActiveLayer.Paste
            Call SizeToFit
            ActiveSelection.ConvertToBitmap , , , , 600
            ActivePage.Layers("Design").Printable = False
        End If
    ElseIf AppList.Value = "Adobe Illustrator" Then
        If Illustrator.ActiveDocument.PageItems.Count > 0 Then
            Dim txtsh As Illustrator.TextFrame, sh As Illustrator.PathItem
            For Each txtsh In Illustrator.ActiveDocument.TextFrames
                txtsh.CreateOutline
            Next
            For Each sh In Illustrator.ActiveDocument.PathItems
                sh.Selected = True
            Next
            Illustrator.ActiveDocument.Copy
            PasteAIFormat
        End If
        If ActiveSelection.Shapes.Count > 0 Then
            Call SetOutlineToScale
            ActiveSelection.Group
            Call SizeToFit
            CorelScript.AlignToCenterOfPage 3, 3
            ActiveSelection.Ungroup
        End If
    End If
    Clipboard.Clear
    Unload Me
End Sub
As for Illustrator's scripting support...I noticed Photoshop's object library reference when I first installed CS. I also saw Illustrator 11's type library reference, but assumed it was similar to photoshop's and never checked it out. After seeing your new PasteAIFormat code I decided to give it a second look. To me it seems illustrator's type library compared to photoshop, contains the type & object libraries. The file I believe is a plug-in (ScriptingSupport.aip) found under "C:\Program Files\Adobe\Illustrator CS\Plug-ins\Extensions" on my installation.

Hopefully I've explained my problem well enough, let me know if you see anything wrong or have any ideas. Thanks for the quick reply.

P.S. You might find this useful , if you don't have it already.
http://partners.adobe.com/asn/illust...ptingGuide.pdf
Reply With Quote
  #10  
Old 09-02-2004, 22:48
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,941
Blog Entries: 4
Default Re: Illustrator

Mark,

I believe your problem is in implicit Application object creation.

Whenever you reference a global property of the Application object, like ActiveDocument in your example, the Application object will be created for you automatically. This reference is cached by your application until it quits, so once you connected to it, you cannot actually disconnect and if you close the application manually from the Illustrator or Photoshop then try to re-run your code it will try to reuse the old reference which is no longer there (that is, by the way, a mistake on Adobe's part - they should not quit until all the external references are gone, but that's another conversation).

Anyway, to resolve the issue, you must use an explicit object instantiation. You must declare an application variable yourself and set its value to something. For example:

Code:
Sub YourTestRun()
    Dim illy As Illustrator.Application
    ....
    Set illy = New Illustrator.Application ' This actually creates the application object
    ... ' Use the app reference as you please
   If illy.ActiveDocument.Count >0 Then ...
   ...
   Set illy = Nothing ' kill the reference
   ....
End Sub
You must always be very careful about any object references you create when you connect from out-of-process.

The same applies to photoshop (as does it apply to CorelDRAW but at least CorelDRAW will not shut down on you and kill the links to its automation client).

I hope this helps.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to get my Time Recording script running... johndankey CorelDRAW/Corel DESIGNER VBA 12 26-04-2004 22:48
Accesing CorelDraw 10 VBA Object Model without running it dolmos CorelDRAW/Corel DESIGNER VBA 5 19-12-2003 07:52
Application and Default Settings MrClark CorelDRAW CS 2 18-12-2003 09:35
takes Draw a long time to start a new document! bbolte CorelDRAW/Corel DESIGNER VBA 5 14-05-2003 09:09
Font Substitution Upon Import RobC CorelDRAW/Corel DESIGNER VBA 4 10-03-2003 23:06


All times are GMT -5. The time now is 23:03.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com