2012年5月18日 (金)

インターネットショートカットからリンクを作ってコピーする。

Webページのエディタにリンク(<a href=URL>TITLE</a>)を書き込むとき、既存のリンクがあれば、それをコピー貼り付けできるので簡単ですが、インターネットショートカットしかない場合は面倒です。
そこでインターネットショートカットからリンク(<a href=URL>ファイルのベース名</a>)を作ってコピーします。

url2a2copy.htaをお気に入りバーなどに置いてインターネットショートカットファイルをドロップします。

url2a2copy.hta インターネットショートカットファイル

<html>
<head>
<script language=vbscript>
Option Explicit
Sub window_onload
Dim fQuoting
Dim k
fQuoting=False
For k=1 To Len(hta.commandLine)
  Select Case Mid(hta.commandLine,k,1)
  Case Chr(34) fQuoting=Not fQuoting
  Case Chr(32) If Not fQuoting Then Exit For
  End Select
Next
Dim Link
Set Link=CreateObject("WScript.Shell").CreateShortCut(Trim(Replace(Mid(hta.commandLine,k+1),"""","")))
document.links(0).href=Link.TargetPath
document.links(0).innerText=CreateObject("Scripting.FileSystemObject").GetBaseName(Link.FullName)
document.execCommand "SelectAll"
document.execCommand "Copy"
close
End Sub
</script>
</head>
<body>
<a href=></a>
</body>
</html>
<hta:application id=hta />

2012年5月12日 (土)

高精度?時計

一応、右下に置いてますが、右上や左上にしたければ、適当に変えてください。

HRClock.hta

<script language=vbscript>
moveto screen.availwidth-110,screen.availheight-25
resizeto 110,25
dim t1,t2,t3
setInterval "proc",10
sub proc
t2=time
if t1=t2 then
  t3=t3+1
  document.title=t1 & "." & Mid(t3,2)
else
  t1=t2
  t3=100
  document.title=t1 & "." & Mid(t3,2)
end if
end sub
</script>
<hta:application minimizebutton=no maximizebutton=no />

2012年5月 6日 (日)

DATE/TIMEコマンドやDATE/TIME環境変数の形式は地域に依存します。(その2)

command.comのdateコマンドはISO形式固定のようです。

for /f "tokens=5 eol=E" %%i in ('"echo.|command /c date"') do echo %%i

for /f "tokens=5 eol=E" %%i in ('"echo.|command /c date"') do set ddd=%%i
echo %ddd%

2012年5月 5日 (土)

ifコマンドの文字列比較はまともに使えない。

rem 半角長音
if aa == aー echo bug

rem 全角長音
if aa == aー echo bug

rem 全角ダッシュ
if aa == a― echo bug

rem ヽ
if aa == aヽ echo bug

rem ゝ
if aa == aゝ echo bug

なので、バッチファイルで文字列処理は全く推奨できません。きっぱり。

2012年5月 4日 (金)

GetIconLocationを代替する。

スクリプトからではGetIconLocationが使えません。その代替法です。

Option Explicit
Dim fso
Dim Shell
Dim Path
Dim FolderItem
Dim Link

Set fso=CreateObject("Scripting.FileSystemObject")
Set Shell=CreateObject("Shell.Application")
For Each Path In WScript.Arguments
  Set FolderItem=Shell.NameSpace(fso.GetParentFolderName(Path)).ParseName(fso.GetFileName(Path))
  Set Link=FolderItem.GetLink
  WScript.Echo FolderItem.ExtendedProperty("{5CBF2787-48CF-4208-B90E-EE5E5D420294},10"),FolderItem.ExtendedProperty("{5CBF2787-48CF-4208-B90E-EE5E5D420294},9")
Next

2012年5月 3日 (木)

選択されたurlをリンクに変えるブックマークレット

urlを選択してブックマークレットを実行すると、そのurlをリンクに変える。

javascript:var t=document.selection.createRange();t.execCommand('CreateLink',false,t.text);

2012年5月 2日 (水)

選択されたurlを開くブックマークレット

urlを選択してブックマークレットを実行すると、そのurlを新しいウィンドウで開く。ieの設定により、新しいタブになる。

javascript:open(document.selection.createRange().text,"_blank")

2012年5月 1日 (火)

「このページの検索」を代替、拡張するブックマークレット(その2)

該当箇所の背景色を黄色と赤で交互に点滅させて目立たせます。setIntervalでスクリプトを繰り返し実行しています。停止するには、「最新の情報に更新」。

javascript:var s=prompt('検索文字列','');if(s){var t=true;setInterval('f();',1000)}function f(){var k=0,r=document.body.createTextRange();while(r.findText(s)){k++;r.execCommand('BackColor',false,t?'yellow':'red');r.collapse(false);}t=!t;}

該当箇所をmarqueeでスクロールさせて目立たせます。marqueeを停止するには、「最新の情報に更新」。

javascript:var s=prompt('検索文字列','');if(s){var k=0,r=document.body.createTextRange();while(r.findText(s)){k++;r.pasteHTML('<marquee width='+r.boundingWidth+'>'+r.text+'</marquee>');r.collapse(false);}alert(k);}

2012年4月16日 (月)

Excel起動ショートカットを作る。

インストーラが作ったExcel起動ショートカットは引数が指定できないし、Excelファイルをドロップすることもできません。
もし、Excel起動ショートカットを作ろうとすると、インストールパスを指定しなければなりませんが、バージョンによりインストールパスは異なります。
そこで、バージョンに関係しないExcel起動ショートカットを作ります。

リンク先
rundll32.exe shell32.dll,ShellExec_RunDLL excel.exe

これなら引数を指定できるし、Excelファイルをドロップすることもできます。

アイコンはmoricons.dllの中から選ぶとよいでしょう。

2012年2月21日 (火)

ローカルファイルの履歴を削除する。

Win32APIやNETはホントよくわかりません。見よう見まねです。苦労しました。

DeleteHist.vb

Imports System.Runtime.InteropServices
'Class for deleting the cache.
Public Class Class1
        'For PInvoke: Contains information about an entry in the Internet cache
        <StructLayout(LayoutKind.Explicit, Size:=80)> _
        Public Structure INTERNET_CACHE_ENTRY_INFOA
        <FieldOffset(0)> Public dwStructSize As UInt32
        <FieldOffset(4)> Public lpszSourceUrlName As String
        <FieldOffset(8)> Public lpszLocalFileName As String
        <FieldOffset(12)> Public CacheEntryType As UInt32
        <FieldOffset(16)> Public dwUseCount As UInt32
        <FieldOffset(20)> Public dwHitRate As UInt32
        <FieldOffset(24)> Public dwSizeLow As UInt32
        <FieldOffset(28)> Public dwSizeHigh As UInt32
        <FieldOffset(32)> Public LastModifiedTime As Long
        <FieldOffset(40)> Public ExpireTime As Long
        <FieldOffset(48)> Public LastAccessTime As Long
        <FieldOffset(56)> Public LastSyncTime As Long
        <FieldOffset(64)> Public lpHeaderInfo As IntPtr
        <FieldOffset(68)> Public dwHeaderInfoSize As UInt32
        <FieldOffset(72)> Public lpszFileExtension As IntPtr
        <FieldOffset(76)> Public dwReserved As UInt32
        <FieldOffset(76)> Public dwExemptDelta As UInt32
    End Structure

   'For PInvoke: Begins the enumeration of the Internet cache
   <DllImport("wininet.dll", _
       SetLastError:=True, _
       CharSet:=CharSet.Auto, _
       EntryPoint:="FindFirstUrlCacheEntryA", _
       CallingConvention:=CallingConvention.StdCall)> _
    Shared Function FindFirstUrlCacheEntry( _
   <MarshalAs(UnmanagedType.LPStr)> ByVal lpszUrlSearchPattern As String, _
         ByVal lpFirstCacheEntryInfo As IntPtr, _
         ByRef lpdwFirstCacheEntryInfoBufferSize As Int32) As IntPtr
    End Function

    'For PInvoke: Retrieves the next entry in the Internet cache
    <DllImport("wininet.dll", _
       SetLastError:=True, _
       CharSet:=CharSet.Auto, _
       EntryPoint:="FindNextUrlCacheEntryA", _
       CallingConvention:=CallingConvention.StdCall)> _
    Shared Function FindNextUrlCacheEntry( _
          ByVal hFind As IntPtr, _
          ByVal lpNextCacheEntryInfo As IntPtr, _
          ByRef lpdwNextCacheEntryInfoBufferSize As Integer) As Boolean
    End Function

    'For PInvoke: Removes the file that is associated with the source name from the cache, if the file exists
    <DllImport("wininet.dll", _
      SetLastError:=True, _
      CharSet:=CharSet.Auto, _
      EntryPoint:="DeleteUrlCacheEntryW", _
      CallingConvention:=CallingConvention.StdCall)> _
    Shared Function DeleteUrlCacheEntry( _
        ByVal lpszUrlName As String) As Boolean
    End Function

End Class
    
'次のコードを Module1.vb モジュールに追加:
'Imports System.Runtime.InteropServices
Module Module1

    Sub Main()
        'No more items have been found.
        'File not found.
        Const ERROR_FILE_NOT_FOUND As Integer = &H2
        Const ERROR_NO_MORE_ITEMS As Integer = 259
        Const NORMAL_CACHE_ENTRY As Integer = 1
        Const URLHISTORY_CACHE_ENTRY As Integer = &H200000
        'Local variables
        Dim cacheEntryInfoBufferSizeInitial As Integer = 0
        Dim cacheEntryInfoBufferSize As Integer = 0
        Dim cacheEntryInfoBuffer As IntPtr = IntPtr.Zero
        Dim internetCacheEntry As Class1.INTERNET_CACHE_ENTRY_INFOA
        Dim enumHandle As IntPtr = IntPtr.Zero
        Dim returnValue As Boolean = False
        Dim LastVisited As DateTime

        enumHandle = Class1.FindFirstUrlCacheEntry("Visited:", IntPtr.Zero, cacheEntryInfoBufferSizeInitial)
        If (enumHandle.Equals(IntPtr.Zero) And ERROR_NO_MORE_ITEMS.Equals(Marshal.GetLastWin32Error())) Then
            Exit Sub
        End If

        cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial
        cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize)
        enumHandle = Class1.FindFirstUrlCacheEntry("Visited:", cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
        Console.WriteLine("""LastVisited"",""URL""")
        While (True)
            internetCacheEntry = CType(Marshal.PtrToStructure(cacheEntryInfoBuffer, GetType(Class1.INTERNET_CACHE_ENTRY_INFOA)), Class1.INTERNET_CACHE_ENTRY_INFOA)
            cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize
            If (internetCacheEntry.CacheEntryType = URLHISTORY_CACHE_ENTRY + NORMAL_CACHE_ENTRY) Then
                If (internetCacheEntry.lpszSourceUrlName.Contains("file://")) Then
                    LastVisited=DateTime.FromFileTime(internetCacheEntry.LastAccessTime)
                    Console.WriteLine("""{1}"",""{0}""",internetCacheEntry.lpszSourceUrlName,LastVisited)
                    returnValue = Class1.DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName)
                    If (Not returnValue AndAlso Not ERROR_FILE_NOT_FOUND.Equals(Marshal.GetLastWin32Error())) Then
                        Console.WriteLine("Error Deleting: {0}", Marshal.GetLastWin32Error())
                    End If
                End If
            End If

            returnValue = Class1.FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
            If (Not returnValue And ERROR_NO_MORE_ITEMS.Equals(Marshal.GetLastWin32Error())) Then
                Exit While
            End If

            If (Not returnValue And cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize) Then
                cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial
                Dim tempIntPtr As New IntPtr(cacheEntryInfoBufferSize)
                cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, tempIntPtr)
                returnValue = Class1.FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
            End If
        End While
        Marshal.FreeHGlobal(cacheEntryInfoBuffer)

    End Sub

End Module

«固定サイトショートカット(.website)のコンテキストメニューに「編集」、プロパティシートに「Webドキュメント」タブを追加する。